blob: 6f7858db00e040a205657981d3e78c51d7d0cb36 (
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
|
# NAME
# krops.nix - deployment entry point
#
# SYNOPSIS
# $(nix-build krops.nix --no-out-link --argstr name HOSTNAME -A ACTION)
#
# ACTIONS
# deploy build the system on the target and switch to it
# diff build the system on the target and show what switching to it
# would change
#
{ lib ? import ~/sync/src/krops/lib
, pkgs ? import ~/sync/src/krops/pkgs {}
, name
, target ? null
}@args: rec {
spec = {
nixpkgs = "nixos-26.05";
target = "root@${name}.r/var/src";
} // import ./systems/${name}/spec.nix;
target = if args.target or null != null then args.target else spec.target;
source = lib.evalSource [
{
hrm.file = {
path = toString ~/sync/src/hrm;
};
nixos-config.symlink = {
target = "hrm/systems/${name}/config.nix";
};
nixpkgs.git = {
ref = (lib.importJSON ./nixpkgs.json).${spec.nixpkgs}.rev;
url = "https://github.com/NixOS/nixpkgs";
};
secrets.passage = {
age = "${pkgs.rage}/bin/rage";
dir = toString ~/sync/secrets/${name};
identities_file = toString ~/.passage/identities;
};
stockholm.file = {
path = toString ~/sync/src/stockholm;
};
}
];
deploy = hrm.legacy.deploy;
diff = hrm.legacy.diff;
hrm.legacy.deploy = mkAction "legacy" "deploy";
hrm.legacy.diff = mkAction "legacy" "diff";
hrm.flake.deploy = mkAction "flake" "deploy";
hrm.flake.diff = mkAction "flake" "diff";
mkAction = build-method: deploy-action: let
target' = lib.mkTarget target;
name' = "${name}-deploy-${build-method}-${deploy-action}";
in
pkgs.writers.writeDash name' /* sh */ ''
set -efu
${pkgs.populate { source = source; target = target'; }}
${pkgs.krops.runShell target' { allocateTTY = true; } /* sh */ ''
set -efu
system=$(
export NIX_PATH=${lib.escapeShellArg target'.path}
${builtins.getAttr build-method {
legacy = /* sh */ ''
nix-build \
-j 1 \
--show-trace \
--attr nixosConfigurations.${name}.config.system.build.toplevel \
'<hrm/legacy.nix>'
'';
flake = /* sh */ ''
nix build \
--print-build-logs \
--show-trace \
--print-out-paths \
"$NIX_PATH"/hrm#nixosConfigurations.${name}.config.system.build.toplevel
'';
}}
)
${builtins.getAttr deploy-action {
deploy = /* sh */ ''
old_gen=$(readlink -f /nix/var/nix/profiles/system)
new_gen=$(readlink -f "$system")
if [ "x$old_gen" != "x$new_gen" ]; then
nix-env -p /nix/var/nix/profiles/system --set "$system"
"$system"/bin/switch-to-configuration switch
else
echo 'Configuration is already up to date' >&2
fi
'';
diff = /* sh */ ''
nix --extra-experimental-features nix-command \
store diff-closures /run/current-system "$system"
if command -v nix-diff >/dev/null; then
nix-diff --environment --skip-already-compared /run/current-system "$system"
else
NIX_PATH=${lib.escapeShellArg target'.path} \
nix-shell -p nix-diff --run \
"nix-diff --environment --skip-already-compared /run/current-system $system"
fi
"$system"/bin/switch-to-configuration dry-activate
'';
}}
''}
'';
}
|