aboutsummaryrefslogtreecommitdiff
path: root/flake.nix
blob: 6b7110a553add9d9222a18fd62aff25051ee3055 (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# ldgallery - A static generator which turns a collection of tagged
#             pictures into a searchable web gallery.
#
# Copyright (C) 2019-2022  Pacien TRAN-GIRARD
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

{
  description = "A static web gallery generator with tags";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";
    flaky-utils.url = "git+https://cgit.pacien.net/libs/flaky-utils";
  };

  outputs = { self, nixpkgs, flake-utils, flaky-utils }:
  flake-utils.lib.eachDefaultSystem (system: let
    pkgs = nixpkgs.legacyPackages.${system};
    ldgalleryVersion = "2.1";
    devTools = with pkgs; [
      # generic
      tmux

      # viewer
      nodejs-16_x
      yarn

      # compiler
      stack
    ];

  in pkgs.lib.fold pkgs.lib.recursiveUpdate { } [
  (rec {
    packages = rec {
      compiler = pkgs.haskell.lib.compose.overrideCabal (super: {
        pname = "ldgallery-compiler";
        version = ldgalleryVersion;

        buildTools = (super.buildTools or []) ++ [ pkgs.makeWrapper ];

        postInstall = ''
          ${super.postInstall or ""}

          # wrapper for runtime dependencies registration
          wrapProgram "$out/bin/ldgallery" \
            --prefix PATH : ${pkgs.lib.makeBinPath [ pkgs.imagemagick ]}

          # bash completion
          mkdir -p "$out/share/bash-completion/completions"
          "$out/bin/ldgallery" \
            --help=bash \
            > "$out/share/bash-completion/completions/ldgallery"
        '';
      }) (pkgs.haskellPackages.callCabal2nix "" ./compiler { });

      viewer = pkgs.mkYarnPackage {
        pname = "ldgallery-viewer";
        version = ldgalleryVersion;
        src = ./viewer;

        buildPhase = ''
          # Make the node_module directory writable because ESLint and Webpack
          # want to write in it during the build…
          mv deps/ldgallery-viewer/node_modules{,links}
          mkdir deps/ldgallery-viewer/node_modules
          cp -r deps/ldgallery-viewer/node_modules{links/.bin,}

          export HOME=/build
          yarn --offline run lint
          yarn --offline run build
        '';

        installPhase = ''
          mkdir -p $out/share/ldgallery
          mv deps/ldgallery-viewer/dist $out/share/ldgallery/viewer
        '';

        doDist = false;  # no need to generate a source tarball
      };

      man = pkgs.stdenv.mkDerivation {
        pname = "ldgallery-man";
        version = ldgalleryVersion;
        src = ./.;

        nativeBuildInputs = with pkgs; [ pandoc ];
        installPhase = ''
          mkdir -p $out/share/man/man{1,7}

          pandoc --standalone --to man \
            "compiler/ldgallery.1.md" \
            --output "$out/share/man/man1/ldgallery.1"

          pandoc --standalone --to man \
            "viewer/ldgallery-viewer.7.md" \
            --output "$out/share/man/man7/ldgallery-viewer.7"

          pandoc --standalone --to man \
            "ldgallery-quickstart.7.md" \
            --output "$out/share/man/man7/ldgallery-quickstart.7"
        '';
      };

      # compiler + viewer + man pages bundle
      ldgallery = pkgs.symlinkJoin {
        name = "ldgallery";
        version = ldgalleryVersion;
        paths = [
          man
          (with pkgs.haskell.lib.compose; overrideCabal (super: {
            prePatch = ''
              # add viewer dist to compiler bundled resources
              rm data/readme.md
              ln -s "${viewer}/share/ldgallery/viewer" "data/"
              ${super.prePatch or ""}
            '';
          }) (justStaticExecutables compiler))
        ];
      };

      example = pkgs.stdenv.mkDerivation {
        pname = "ldgallery-example";
        version = ldgalleryVersion;
        src = ./example;
        nativeBuildInputs = [ ldgallery ];
        buildPhase = ''
          ldgallery --input-dir src --output-dir $out --with-viewer
        '';
        installPhase = ":";
      };

      default = ldgallery;
    };

    apps = rec {
      ldgallery = flake-utils.lib.mkApp {
        drv = packages.default;
      };

      default = ldgallery;
    };

    devShell = flaky-utils.lib.mkDevShell {
      inherit pkgs;
      tools = devTools;
      shell = null;
    };
  })

  (flaky-utils.lib.mkSandboxSystem {
    inherit pkgs;
    restrictNetwork = false;
    patchQemu9p = true;
    tools = devTools;
    envVars = {
      # File modification watch doesn't work through the VM for live reload.
      VUE_APP_WEBPACK_WATCH_POLL = "1000";
    };
    config = {
      # The viewer's build and devel server are resource-hungry.
      virtualisation.cores = 2;
      virtualisation.memorySize = 2 * 1024;

      virtualisation.forwardPorts = [
        { from = "host"; host.port = 8085; guest.port = 8085; }  # vue-cli
      ];
    };
  })

  ]);
}