aboutsummaryrefslogtreecommitdiffstats
path: root/tests/pure.nix
blob: ce540b4b9e282e286f464c8df7c90228ebacd400 (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
# Flakes evaluating krops purely, as flakes evaluate by default, and scripts
# running them on the deploying machine.  Their inputs are krops' and
# nixpkgs' sources in the store, without .git, locked as tarballs by their
# hashes, so Nix uses the store paths as they are, without fetching or
# reading them, and the VM shares them with the host's store.
{ pkgs }:

let
  inherit (pkgs) lib;
  inherit (pkgs.stdenv.hostPlatform) system;

  kropsSrc = builtins.path {
    name = "source";
    path = ../.;
    filter = path: type: let name = baseNameOf path; in
      name != ".git" && name != "target" && !lib.hasPrefix "result" name;
  };
  nixpkgsSrc = builtins.path {
    name = "source";
    path = pkgs.path;
    filter = path: type: baseNameOf path != ".git";
  };
  nixpkgsLibSrc = builtins.path {
    name = "source";
    path = pkgs.path + "/lib";
  };

  # A directory with flake.nix, whose inputs are the store paths of inputs
  # by name, not flakes, its lock file, and files, by name.
  flake = { inputs, outputs, files ? {} }: pkgs.runCommand "pure-flake" {
    nativeBuildInputs = [ pkgs.nix ];
  } ''
    mkdir $out
    cat > $out/flake.nix <<'EOF'
    {
      ${lib.concatStrings (lib.mapAttrsToList (name: _: ''
        inputs.${name} = { url = "file:///${name}.tar.gz"; flake = false; };
      '') inputs)}
      outputs = ${outputs};
    }
    EOF
    export HOME=$TMPDIR
    {
      echo '{ "nodes": { "root": { "inputs": {'
      ${lib.concatStringsSep "\necho ,\n" (lib.mapAttrsToList (name: _: ''
        printf '"%s": "%s"' ${name} ${name}
      '') inputs)}
      echo '} }'
      ${lib.concatStrings (lib.mapAttrsToList (name: path: ''
        printf ', "%s": { "flake": false, "locked": { "type": "tarball", "url": "file:///%s.tar.gz", "narHash": "%s" }, "original": { "type": "tarball", "url": "file:///%s.tar.gz" } }\n' \
            ${name} ${name} "$(nix-hash --type sha256 --sri ${path})" ${name}
      '') inputs)}
      echo '}, "root": "root", "version": 7 }'
    } > $out/flake.lock
    ${lib.concatStrings (lib.mapAttrsToList (name: file: ''
      cp ${file} $out/${name}
    '') files)}
  '';

  # A script evaluating what a flake's attribute attr is, purely, printing
  # it, and referring to the store paths of the inputs, so the VM has them.
  evaluate = name: dir: attr: inputs: pkgs.writers.writeDash name ''
    set -efu
    # ${toString (lib.attrValues inputs)}
    exec nix eval --raw ${lib.escapeShellArg "path:${dir}#${attr}"}
  '';

  # krops' lib, from a flake with nixpkgs' lib only.  mkTarget leaves the
  # user and the host null, which pure evaluation can't tell, and the
  # sources of each type evaluate.
  libFlake = let
    inputs = { krops = kropsSrc; lib = nixpkgsLibSrc; };
  in flake {
    inherit inputs;
    outputs = /* nix */ ''
      { krops, lib, ... }: let
        klib = import "''${krops}/lib/with.nix" { lib = import lib; };
        target = klib.mkTarget "";
      in {
        check =
          assert target.user == null && target.host == null;
          assert target.path == "/var/src";
          builtins.toJSON (klib.evalSource [{
            a.derivation = "{}";
            b.file = "/fixtures/alt";
            c.flake = "/fixtures/top";
            d.flake-input = { url = "/fixtures/top"; name = "dep"; };
            e.git = { url = "git://deployer/gitsrc.git"; ref = "HEAD"; };
            f.pass = { dir = "/fixtures/pass"; name = "x"; };
            g.passage.dir = "/fixtures/passage/store";
            h.pipe = "/fixtures/pipe";
            i.symlink = "b";
          }]);
      }
    '';
  };

  # What the host makes of writeDeploy's arguments args, as the flake.
  deployOf = args: import ./pure-deploy.nix {
    krops = kropsSrc;
    nixpkgs = nixpkgsSrc;
    inherit system args;
  };

in {
  lib = evaluate "pure-lib" libFlake "check" { inherit kropsSrc nixpkgsLibSrc; };

  # A script deploying with writeDeploy's arguments args, which have to be
  # JSON, from a flake evaluating it purely.  Building it in the VM would
  # need what building populate needs, so it runs what the host built of
  # the same derivation, failing if pure evaluation makes another.
  deploy = args: let
    args' = builtins.fromJSON (builtins.toJSON args);
    expected = deployOf args';
    dir = flake {
      inputs = { krops = kropsSrc; nixpkgs = nixpkgsSrc; };
      outputs = /* nix */ ''
        { krops, nixpkgs, ... }: {
          deploy = import ./pure-deploy.nix {
            inherit krops nixpkgs;
            system = ${builtins.toJSON system};
            args = builtins.fromJSON (builtins.readFile ./args.json);
          };
        }
      '';
      files = {
        "args.json" = pkgs.writeText "args.json" (builtins.toJSON args');
        "pure-deploy.nix" = ./pure-deploy.nix;
      };
    };
    drvPath = evaluate "pure-deploy-drv" dir "deploy.drvPath" {
      inherit kropsSrc nixpkgsSrc;
    };
  in pkgs.writers.writeDash "pure-deploy" ''
    set -efu
    drv=$(${drvPath})
    if test "$drv" != ${builtins.unsafeDiscardStringContext expected.drvPath}; then
      >&2 printf 'pure evaluation makes %s instead of %s\n' "$drv" \
          ${builtins.unsafeDiscardStringContext expected.drvPath}
      exit 1
    fi
    exec ${expected}
  '';
}