nixfiles/nixos/modules/deploy-rs.nix

151 lines
4.7 KiB
Nix
Raw Permalink Normal View History

{ lib, pkgs, config, systems, ... }:
2022-02-19 22:55:53 +00:00
let
inherit (builtins) head attrNames;
inherit (lib) mkMerge mkIf mkDefault optionalAttrs mapAttrs' optionalString;
2022-02-19 22:55:53 +00:00
inherit (lib.my) mkOpt' mkBoolOpt';
cfg = config.my.deploy;
keepGensOpt = with lib.types; mkOpt' ints.unsigned 10
"Number of generations to keep when cleaning up old deployments (0 to disable deletion on deployment).";
keepGensSnippet = p: n: optionalString (n > 0) ''
${config.nix.package}/bin/nix-env -p "${p}" --delete-generations +${toString n}
'';
# Based on https://github.com/serokell/deploy-rs/blob/master/flake.nix
nixosActivate = cfg': base: (pkgs.deploy-rs.lib.activate.custom // {
dryActivate = "$PROFILE/bin/switch-to-configuration dry-activate";
2023-12-28 18:33:55 +00:00
boot = ''
$PROFILE/bin/switch-to-configuration boot
${keepGensSnippet "$PROFILE" cfg'.keepGenerations}
'';
}) base.config.system.build.toplevel ''
# work around https://github.com/NixOS/nixpkgs/issues/73404
cd /tmp
2023-12-28 18:33:55 +00:00
"$PROFILE"/bin/switch-to-configuration switch
# https://github.com/serokell/deploy-rs/issues/31
${with base.config.boot.loader;
2023-12-28 18:33:55 +00:00
optionalString systemd-boot.enable
"sed -i '/^default /d' ${efi.efiSysMountPoint}/loader/loader.conf"}
${keepGensSnippet "$PROFILE" cfg'.keepGenerations}
'';
systemdUtil = pkgs.writeShellApplication {
name = "systemd-util.sh";
text = ''
svcActionWatch() {
action="$1"
shift
unit="$1"
shift
journalctl -o cat --no-pager -n 0 -f -u "$unit" &
jPid=$!
cleanup() {
# shellcheck disable=SC2317
kill "$jPid"
}
trap cleanup EXIT
systemctl "$@" "$action" "$unit"
}
'';
};
ctrProfiles = optionalAttrs cfg.generate.containers.enable (mapAttrs' (n: c:
let
ctrConfig = systems."${n}".configuration.config;
in
{
name = "container-${n}";
value = {
2023-12-28 18:33:55 +00:00
path = (pkgs.deploy-rs.lib.activate.custom // {
boot = ''
echo "Next systemd-nspawn@${n}.service restart / reload will load config"
'';
}) ctrConfig.my.buildAs.container ''
source ${systemdUtil}/bin/systemd-util.sh
${if c.hotReload then ''
if (! systemctl show -p ActiveState systemd-nspawn@${n} | grep -q "ActiveState=active") || \
systemctl show -p StatusText systemd-nspawn@${n} | grep -q "Dummy container"; then
2022-05-31 21:25:51 +01:00
action=restart
else
action=reload
fi
svcActionWatch "$action" systemd-nspawn@${n}
'' else ''
svcActionWatch restart systemd-nspawn@${n}
''}
${keepGensSnippet "$PROFILE" cfg.generate.containers.keepGenerations}
'';
profilePath = "/nix/var/nix/profiles/per-container/${n}/system";
user = "root";
};
}) config.my.containers.instances);
2022-02-19 22:55:53 +00:00
in
{
options.my.deploy = with lib.types; {
2022-02-19 22:55:53 +00:00
authorizedKeys = {
keys = mkOpt' (listOf singleLineStr) [ ] "SSH public keys to add to the default deployment user.";
2023-11-02 13:41:50 +00:00
keyFiles = mkOpt' (listOf path) [ lib.my.c.sshKeyFiles.deploy ] "SSH public key files to add to the default deployment user.";
2022-02-19 22:55:53 +00:00
};
enable = mkBoolOpt' true "Whether to expose deploy-rs configuration for this system.";
inherit (lib.my.deploy-rs) node;
2022-02-19 22:55:53 +00:00
generate = {
system = {
enable = mkBoolOpt' true "Whether to generate a deploy-rs profile for this system's config.";
mode = mkOpt' str "switch" "switch-to-configuration mode.";
keepGenerations = keepGensOpt;
};
containers = {
enable = mkBoolOpt' true "Whether to generate deploy-rs profiles for this system's containers.";
keepGenerations = keepGensOpt;
};
2022-02-19 22:55:53 +00:00
};
};
config = mkMerge [
{
my.deploy.enable = mkIf (config.my.build.isDevVM || config.boot.isContainer) false;
}
(mkIf cfg.enable {
my.deploy.node = {
hostname = mkDefault config.networking.fqdn;
profilesOrder = [ "system" ] ++ (attrNames ctrProfiles);
profiles = {
system = mkIf cfg.generate.system.enable {
path = nixosActivate cfg.generate.system { inherit config; };
2022-02-19 22:55:53 +00:00
user = "root";
2022-02-19 22:55:53 +00:00
};
} // ctrProfiles;
sshUser = "deploy";
user = mkDefault "root";
sudo = mkDefault (if config.security.doas.enable then "doas -u" else "sudo -u");
sshOpts = mkDefault [ "-p" (toString (head config.services.openssh.ports)) ];
2022-02-19 22:55:53 +00:00
};
2022-02-19 22:55:53 +00:00
users = {
users."${cfg.node.sshUser}" = {
isSystemUser = true;
group = cfg.node.sshUser;
extraGroups = mkDefault [ "wheel" ];
shell = pkgs.bash;
openssh.authorizedKeys = cfg.authorizedKeys;
};
groups."${cfg.node.sshUser}" = {};
};
})
];
}