stockholm/lass/krops.nix

146 lines
4.2 KiB
Nix
Raw Normal View History

2018-09-07 17:55:22 +02:00
{ name }: let
2018-06-07 08:24:56 +02:00
inherit (import ../krebs/krops.nix { inherit name; })
2018-04-05 19:31:39 +02:00
krebs-source
lib
pkgs
;
source = { test }: lib.evalSource ([
(krebs-source { test = test; })
2018-04-05 19:31:39 +02:00
{
2018-05-12 15:53:04 +02:00
nixos-config.symlink = "stockholm/lass/1systems/${name}/physical.nix";
2023-06-19 02:16:17 +02:00
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 = {
2023-07-15 17:04:58 +02:00
ref = (lib.importJSON ../krebs/nixpkgs-unstable.json).rev;
2023-06-19 02:16:17 +02:00
url = https://github.com/NixOS/nixpkgs;
shallow = true;
};
});
2018-04-05 19:31:39 +02:00
secrets = if test then {
file = toString ./2configs/tests/dummy-secrets;
2018-04-05 19:31:39 +02:00
} else {
pass = {
2020-01-11 21:03:42 +01:00
dir = "${lib.getEnv "HOME"}/sync/pwstore";
2018-04-05 19:31:39 +02:00
name = "hosts/${name}";
};
};
2021-12-25 09:51:16 +01:00
stockholm.file = lib.mkForce {
path = toString ../.;
useChecksum = true;
};
2018-04-05 19:31:39 +02:00
}
2021-10-23 19:03:02 +02:00
(if lib.pathExists (./. + "/1systems/${name}/source.nix") then
2019-10-14 16:00:07 +02:00
import (./. + "/1systems/${name}/source.nix") { inherit lib pkgs test; }
else
{}
)
]);
2018-04-05 19:31:39 +02:00
in {
2018-11-20 01:16:22 +01:00
2023-09-03 12:35:59 +02:00
deploy = { target ? "root@${name}/var/src", offline ? false, command ? "switch" }: pkgs.krops.writeCommand "deploy" {
2021-10-26 21:00:22 +02:00
command = targetPath: ''
2022-06-21 23:39:33 +02:00
set -xfu
2021-10-26 21:00:22 +02:00
outDir=$(mktemp -d)
trap "rm -rf $outDir;" INT TERM EXIT
2022-06-21 23:39:33 +02:00
build=$(command -v nom-build || echo "nix-build")
$build \
2021-10-26 21:00:22 +02:00
-I "${targetPath}" \
2022-06-21 23:39:33 +02:00
'<nixpkgs/nixos>' -A config.system.build.toplevel \
-o "$outDir/out" \
${lib.optionalString offline "--option substitute false"} \
# -vvvvv --show-trace
2021-10-26 21:00:22 +02:00
nix-env -p /nix/var/nix/profiles/system --set "$outDir/out"
2023-09-03 12:35:59 +02:00
"$outDir/out/bin/switch-to-configuration" ${command}
2021-10-26 21:00:22 +02:00
'';
2018-04-05 19:31:39 +02:00
source = source { test = false; };
2021-10-26 21:00:22 +02:00
allocateTTY = true;
2023-09-03 12:35:59 +02:00
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;
2018-04-05 19:31:39 +02:00
};
2018-11-20 01:16:22 +01:00
# 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" {
2018-10-09 13:48:52 +02:00
force = true;
inherit target;
2018-04-05 19:31:39 +02:00
source = source { test = true; };
2018-09-07 17:40:10 +02:00
};
2023-09-03 12:35:59 +02:00
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;
};
2018-04-05 19:31:39 +02:00
}