summaryrefslogtreecommitdiffstats
path: root/lass/krops.nix
blob: 407df3bc6376415af0ceb637d77c6976728c5ac5 (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
{ name }: let
  inherit (import ../krebs/krops.nix { inherit name; })
    krebs-source
    lib
    pkgs
  ;

  source = { test }: lib.evalSource ([
    (krebs-source { test = test; })
    {
      nixos-config.symlink = "stockholm/lass/1systems/${name}/physical.nix";
      nixpkgs = lib.mkForce (if test then {
        derivation = let
          rev = (lib.importJSON ../krebs/nixpkgs-unstable.json).rev;
          sha256 = (lib.importJSON ../krebs/nixpkgs-unstable.json).sha256;
        in ''
          with import (builtins.fetchTarball {
            url = "https://github.com/nixos/nixpkgs/archive/${rev}.tar.gz";
            sha256 = "${sha256}";
          }) {};
          pkgs.fetchFromGitHub {
            owner = "nixos";
            repo = "nixpkgs";
            rev = "${rev}";
            sha256 = "${sha256}";
          }
        '';
      } else {
        git = {
          ref = (lib.importJSON ../krebs/nixpkgs-unstable.json).rev;
          url = https://github.com/NixOS/nixpkgs;
          shallow = true;
        };
      });
      secrets = if test then {
        file = toString ./2configs/tests/dummy-secrets;
      } else {
        pass = {
          dir = "${lib.getEnv "HOME"}/sync/pwstore";
          name = "hosts/${name}";
        };
      };
      stockholm.file = lib.mkForce {
        path = toString ../.;
        useChecksum = true;
      };
    }
    (if lib.pathExists (./. + "/1systems/${name}/source.nix") then
      import (./. + "/1systems/${name}/source.nix") { inherit lib pkgs test; }
    else
      {}
    )
  ]);

in {

  deploy = { target ? "root@${name}/var/src", offline ? false, command ? "switch" }: pkgs.krops.writeCommand "deploy" {
    command = targetPath: ''

      set -xfu

      outDir=$(mktemp -d)
      trap "rm -rf $outDir;" INT TERM EXIT

      build=$(command -v nom-build || echo "nix-build")

      $build \
        -I "${targetPath}" \
        '<nixpkgs/nixos>' -A config.system.build.toplevel \
        -o "$outDir/out" \
        ${lib.optionalString offline "--option substitute false"} \
        # -vvvvv --show-trace

      nix-env -p /nix/var/nix/profiles/system --set "$outDir/out"

      "$outDir/out/bin/switch-to-configuration" ${command}
    '';
    source = source { test = false; };
    allocateTTY = true;
    backup = false;
    inherit target;
  };

  deployWithFlake = { target ? "root@${name}/var/src", offline ? false }: pkgs.krops.writeCommand "deploy" {
    source = {
      inherit (source { test = false; }) stockholm secrets;
    };
    command = targetPath: ''
    '';
    allocateTTY = true;
    inherit target;
  };

  # usage: $(nix-build --no-out-link --argstr name HOSTNAME --argstr target PATH -A populate)
  populate = { target, force ? false }: pkgs.populate {
    inherit force;
    source = source { test = false; };
    target = lib.mkTarget target;
  };

  # usage: $(nix-build --no-out-link --argstr name HOSTNAME --argstr target PATH -A test)
  test = { target }: pkgs.krops.writeTest "${name}-test" {
    force = true;
    inherit target;
    source = source { test = true; };
  };

  deploy-with-diff = { target ? "root@${name}/var/src" }: pkgs.krops.writeCommand "${name}-deploy" {
    command = targetPath: ''
      set -xu
      deployScript=$(mktemp)
      cat << EOF > "$deployScript"
      #! /usr/bin/env nix-shell
      #! nix-shell -p nix-diff proot rsync -i bash
      set -xfu

      oldPath=\$(echo "${targetPath}" | sed 's/-new$//')
      oldSystemDrv=\$(nix show-derivation /run/current-system | jq -r 'keys[0]')
      newSystemDrv=\$(proot -b /var/src-new:/var/src nix-instantiate -I /var/src '<nixpkgs/nixos>' -A config.system.build.toplevel)

      (
        diff -rq -x '.git' "\$oldPath" "${targetPath}"
        nix-diff --color always --line-oriented "\$oldSystemDrv" "\$newSystemDrv"
      ) | less -R
      echo 'continue? [(Y)es]/(n)o'
      read yn
      case \$yn in
        [Nn]* ) exit;;
      esac
      rsync -ra --delete /var/src-new/ /var/src/
      nixos-rebuild -I /var/src switch
      EOF

      chmod +x "$deployScript"
      echo "$deployScript"
      cat "$deployScript"
      exec "$deployScript"
      rm "$deployScript"
    '';
    target = "${target}-new";
    source = source { test = false; };
    force = true;
    allocateTTY = true;
  };
}