summaryrefslogtreecommitdiffhomepage
path: root/gleam/flake.nix
blob: 49cd3f137162617a16995e451b3fcbd7b2126a1f (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
{
  description = "ocda-write: sample writers over the object-centric document abstraction, in Gleam";

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

  /* The library this is built on, as a source tree rather than a flake.
     gleam.toml names it as the path dependency ./ocda-lib, which in a
     working tree is a symlink to the library's gleam/ and is not carried
     in git; the build is given the library from this input instead, and
     nix-gleam puts it in place as a local package.

     flake.lock pins the commit of the library this writer was built and
     checked against. To build against a working copy of it instead:

       nix build --override-input ocda-lib path:../../sisudoc-ocda-lib
  */
  inputs.ocda-lib = {
    url = "git+git://git.sisudoc.org/tools/sisudoc-ocda-lib";
    flake = false;
  };

  outputs = { self, nixpkgs, ocda-lib }:
    let
      system = "x86_64-linux"; # adjust for aarch64-linux / darwin
      pkgs = import nixpkgs { inherit system; };

      name = "ocda_write";

      /* The builder, nix-gleam's, kept here in nix-gleam/ (see the README
         there). It fetches each Hex dependency on its own, with the
         checksum gleam has already written into manifest.toml, so there is
         no hash in this file to keep up to date, and nothing a run of gleam
         could make come out differently.
      */
      inherit (pkgs.callPackage ./nix-gleam { }) buildGleamApplication;

      /* rebar3 with the port compiler already in it.

         esqlite builds a NIF, and its rebar3 configuration asks for the
         `pc` plugin, which rebar3 would otherwise fetch from Hex at build
         time: impossible in a sandbox, and the build fails with
         "missing_package pc". nixpkgs carries the plugin, so the answer is
         a rebar3 that has it before it starts.
      */
      rebar3-pc = pkgs.beamPackages.rebar3WithPlugins {
        plugins = [ pkgs.beamPackages.pc ];
      };
    in {
      packages.${system} = {
        default = self.packages.${system}.${name};

        /* Shipped as an Erlang shipment rather than as an escript, because
           sqlite on the BEAM is a NIF: esqlite compiles its own sqlite into
           a shared object, and a NIF cannot be loaded from inside an
           escript archive, the runtime needing a real file on disk. The
           shipment keeps priv/ on disk, so it loads.
        */
        ${name} = buildGleamApplication {
          pname = "ocda-write";
          version = "2.0.0";
          src = ./.;
          localPackages = [ "${ocda-lib}/gleam" ];
          rebar3Package = rebar3-pc;
          nativeBuildInputs = [ pkgs.makeWrapper ];
          preBuild = ''
            export HOME=$TMPDIR
          '';
          installPhase = ''
            runHook preInstall
            mkdir -p $out/lib $out/bin
            cp -r build/erlang-shipment $out/lib/${name}
            makeWrapper $out/lib/${name}/entrypoint.sh $out/bin/ocda-write \
              --add-flags run \
              --prefix PATH : ${pkgs.lib.makeBinPath [ pkgs.beamPackages.erlang pkgs.coreutils ]}
            runHook postInstall
          '';
          meta = {
            description = "writes a document out from either serialisation of its abstraction";
            mainProgram = "ocda-write";
          };
        };
      };

      devShells.${system}.default = pkgs.mkShell {
        packages = [ pkgs.gleam pkgs.beamPackages.erlang pkgs.rebar3 pkgs.sqlite ];
      };
    };
}