aboutsummaryrefslogtreecommitdiff
path: root/flake.nix
blob: 7fda0553fd13d638b9ff0b415f73dc4199172e65 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
{
  description = "Benzin - Wii BRlyt/BRlan/TPL file tools";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  };

  outputs =
    { self, nixpkgs }:
    let
      systems = [
        "x86_64-linux"
        "aarch64-linux"
      ];
      forAllSystems = nixpkgs.lib.genAttrs systems;

      mkPkg =
        {
          pkgs,
          pname,
          version,
          srcDir,
          buildInputs,
          makefile,
          description,
          extraPatchPhase ? "",
        }:
        pkgs.stdenv.mkDerivation {
          inherit
            pname
            version
            buildInputs
            description
            ;

          src = if srcDir == "." then self else "${self}/${srcDir}";

          patchPhase = ''
            substituteInPlace ${makefile} --replace-fail 'gcc' '$(CC)'
            ${extraPatchPhase}
          '';

          installPhase = ''
            mkdir -p $out/bin
            cp ${pname} $out/bin/
          '';

          meta = {
            inherit description;
            homepage = "https://github.com/trap15/benzin";
            platforms = pkgs.lib.platforms.linux;
            maintainers = [ ];
          };
        };
    in
    {
      packages = forAllSystems (
        system:
        let
          pkgs = import nixpkgs { inherit system; };
        in
        {
          benzin = mkPkg {
            inherit pkgs;
            pname = "benzin";
            version = "2.1.11-beta";
            srcDir = ".";
            buildInputs = [ pkgs.minixml ];
            makefile = "Makefile";
            description = "Tool for parsing and creating BRlyt/BRlan files (Wii UI format)";

            extraPatchPhase = ''
              # xml.c uses mxml 2.x struct internals; rewrite to use accessor functions.
              # Remove the redundant assignment on line 35, then fix the for loop.
              sed -i '/^[[:space:]]*current = node->child;$/{
                /for/d
                d
              }' xml.c

              substituteInPlace xml.c \
                --replace-fail 'for (current = node->child; current && ptr < end; current = current->next)' \
                'for (current = mxmlGetFirstChild(node); current && ptr < end; current = mxmlGetNextSibling(current))' \
                --replace-fail 'current->type == MXML_TEXT' 'mxmlGetType(current) == MXML_TEXT' \
                --replace-fail 'current->type == MXML_OPAQUE' 'mxmlGetType(current) == MXML_OPAQUE' \
                --replace-fail 'current->type == MXML_INTEGER' 'mxmlGetType(current) == MXML_INTEGER' \
                --replace-fail 'current->type == MXML_REAL' 'mxmlGetType(current) == MXML_REAL' \
                --replace-fail 'current->value.opaque' 'mxmlGetOpaque(current)' \
                --replace-fail 'current->value.integer' 'mxmlGetInteger(current)' \
                --replace-fail 'current->value.real' 'mxmlGetReal(current)' \
                --replace-fail 'node->value.element.name' 'mxmlGetElement(node)' \
                --replace-fail 'temp = temp->parent' 'temp = mxmlGetParent(temp)' \
                --replace-fail 'node->prev' 'mxmlGetPrevSibling(node)' \
                --replace-fail 'node->parent' 'mxmlGetParent(node)'

              # Handle text node: insert local vars before the whitespace check
              substituteInPlace xml.c \
                --replace-fail 'if (current->value.text.whitespace)' \
                'int _mxml_ws; const char *_mxml_str = mxmlGetText(current, &_mxml_ws); if (_mxml_ws)'

              substituteInPlace xml.c \
                --replace-fail 'current->value.text.string' '_mxml_str'
            '';
          };

          zetsubou = mkPkg {
            inherit pkgs;
            pname = "zetsubou";
            version = "0.1";
            srcDir = "zetsubou";
            buildInputs = [ pkgs.gd ];
            makefile = "Makefile";
            description = "TPL (Texture Palette List) converter — TPL to/from BMP/PNG";
          };

          gentpl = mkPkg {
            inherit pkgs;
            pname = "gentpl";
            version = "0.1";
            srcDir = "gentpl";
            buildInputs = [ pkgs.gd ];
            makefile = "Makefile";
            description = "Simple PNG to TPL converter";
          };

          default = self.packages.${system}.benzin;
        }
      );

      devShells = forAllSystems (
        system:
        let
          pkgs = import nixpkgs { inherit system; };
        in
        {
          default = pkgs.mkShell {
            buildInputs = with pkgs; [
              minixml
              gd
            ];
          };
        }
      );
    };
}