1 { config, lib, pkgs, ... }:
4 cfg = config.services.self-deploy;
6 workingDirectory = "/var/lib/nixos-self-deploy";
7 repositoryDirectory = "${workingDirectory}/repo";
8 outPath = "${workingDirectory}/system";
10 gitWithRepo = "git -C ${repositoryDirectory}";
15 if builtins.isString value
16 then " --argstr ${lib.escapeShellArg key} ${lib.escapeShellArg value}"
17 else " --arg ${lib.escapeShellArg key} ${lib.escapeShellArg (toString value)}";
19 lib.concatStrings (lib.mapAttrsToList toArg args);
21 isPathType = x: lib.types.path.check x;
25 options.services.self-deploy = {
26 enable = lib.mkEnableOption "self-deploy";
28 nixFile = lib.mkOption {
29 type = lib.types.path;
31 default = "/default.nix";
34 Path to nix file in repository. Leading '/' refers to root of
39 nixAttribute = lib.mkOption {
40 type = with lib.types; nullOr str;
45 Attribute of `nixFile` that builds the current system.
49 nixArgs = lib.mkOption {
50 type = lib.types.attrs;
55 Arguments to `nix-build` passed as `--argstr` or `--arg` depending on
60 switchCommand = lib.mkOption {
61 type = lib.types.enum [ "boot" "switch" "dry-activate" "test" ];
66 The `switch-to-configuration` subcommand used.
70 repository = lib.mkOption {
71 type = with lib.types; oneOf [ path str ];
74 The repository to fetch from. Must be properly formatted for git.
76 If this value is set to a path (must begin with `/`) then it's
77 assumed that the repository is local and the resulting service
78 won't wait for the network to be up.
80 If the repository will be fetched over SSH, you must add an
81 entry to `programs.ssh.knownHosts` for the SSH host for the fetch
86 sshKeyFile = lib.mkOption {
87 type = with lib.types; nullOr path;
92 Path to SSH private key used to fetch private repositories over
97 branch = lib.mkOption {
105 Technically speaking any ref can be specified here, as this is
106 passed directly to a `git fetch`, but for the use-case of
107 continuous deployment you're likely to want to specify a branch.
111 startAt = lib.mkOption {
112 type = with lib.types; either str (listOf str);
117 The schedule on which to run the `self-deploy` service. Format
118 specified by `systemd.time 7`.
120 This value can also be a list of `systemd.time 7` formatted
121 strings, in which case the service will be started on multiple
127 config = lib.mkIf cfg.enable {
128 systemd.services.self-deploy = rec {
129 inherit (cfg) startAt;
131 serviceConfig.Type = "oneshot";
133 requires = lib.mkIf (!(isPathType cfg.repository)) [ "network-online.target" ];
137 environment.GIT_SSH_COMMAND = lib.mkIf (cfg.sshKeyFile != null)
138 "${pkgs.openssh}/bin/ssh -i ${lib.escapeShellArg cfg.sshKeyFile}";
140 restartIfChanged = false;
147 ] ++ lib.optionals (cfg.switchCommand == "boot") [ systemd ];
150 if [ ! -e ${repositoryDirectory} ]; then
151 mkdir --parents ${repositoryDirectory}
152 git init ${repositoryDirectory}
155 ${gitWithRepo} fetch ${lib.escapeShellArg cfg.repository} ${lib.escapeShellArg cfg.branch}
157 ${gitWithRepo} checkout FETCH_HEAD
159 nix-build${renderNixArgs cfg.nixArgs} ${lib.cli.toGNUCommandLineShell { } {
160 attr = cfg.nixAttribute;
162 }} ${lib.escapeShellArg "${repositoryDirectory}${cfg.nixFile}"}
164 ${lib.optionalString (cfg.switchCommand != "test")
165 "nix-env --profile /nix/var/nix/profiles/system --set ${outPath}"}
167 ${outPath}/bin/switch-to-configuration ${cfg.switchCommand}
171 ${gitWithRepo} gc --prune=all
173 ${lib.optionalString (cfg.switchCommand == "boot") "systemctl reboot"}