···
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}";
12
+
renderNixArgs = args:
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.strings.isCoercibleToString x && builtins.substring 0 1 (toString 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 = lib.types.str;
43
+
Attribute of `nixFile` that builds the current system.
47
+
nixArgs = lib.mkOption {
48
+
type = lib.types.attrs;
53
+
Arguments to `nix-build` passed as `--argstr` or `--arg` depending on
58
+
switchCommand = lib.mkOption {
59
+
type = lib.types.enum [ "boot" "switch" "dry-activate" "test" ];
64
+
The `switch-to-configuration` subcommand used.
68
+
repository = lib.mkOption {
69
+
type = with lib.types; oneOf [ path str ];
72
+
The repository to fetch from. Must be properly formatted for git.
74
+
If this value is set to a path (must begin with `/`) then it's
75
+
assumed that the repository is local and the resulting service
76
+
won't wait for the network to be up.
78
+
If the repository will be fetched over SSH, you must add an
79
+
entry to `programs.ssh.knownHosts` for the SSH host for the fetch
84
+
sshKeyFile = lib.mkOption {
85
+
type = with lib.types; nullOr path;
90
+
Path to SSH private key used to fetch private repositories over
95
+
branch = lib.mkOption {
96
+
type = lib.types.str;
103
+
Technically speaking any ref can be specified here, as this is
104
+
passed directly to a `git fetch`, but for the use-case of
105
+
continuous deployment you're likely to want to specify a branch.
109
+
startAt = lib.mkOption {
110
+
type = with lib.types; either str (listOf str);
112
+
default = "hourly";
115
+
The schedule on which to run the `self-deploy` service. Format
116
+
specified by `systemd.time 7`.
118
+
This value can also be a list of `systemd.time 7` formatted
119
+
strings, in which case the service will be started on multiple
125
+
config = lib.mkIf cfg.enable {
126
+
systemd.services.self-deploy = {
127
+
wantedBy = [ "multi-user.target" ];
129
+
requires = lib.mkIf (!(isPathType cfg.repository)) [ "network-online.target" ];
131
+
environment.GIT_SSH_COMMAND = lib.mkIf (!(isNull cfg.sshKeyFile))
132
+
"${pkgs.openssh}/bin/ssh -i ${lib.escapeShellArg cfg.sshKeyFile}";
134
+
restartIfChanged = false;
136
+
path = with pkgs; [
143
+
if [ ! -e ${repositoryDirectory} ]; then
144
+
mkdir --parents ${repositoryDirectory}
145
+
git init ${repositoryDirectory}
148
+
${gitWithRepo} fetch ${lib.escapeShellArg cfg.repository} ${lib.escapeShellArg cfg.branch}
150
+
${gitWithRepo} checkout FETCH_HEAD
152
+
nix-build${renderNixArgs cfg.nixArgs} ${lib.cli.toGNUCommandLineShell { } {
153
+
attr = cfg.nixAttribute;
154
+
out-link = outPath;
155
+
}} ${lib.escapeShellArg "${repositoryDirectory}${cfg.nixFile}"}
157
+
${lib.optionalString (cfg.switchCommand != "test")
158
+
"nix-env --profile /nix/var/nix/profiles/system --set ${outPath}"}
160
+
${outPath}/bin/switch-to-configuration ${cfg.switchCommand}
164
+
${gitWithRepo} gc --prune=all
166
+
${lib.optionalString (cfg.switchCommand == "boot") "systemctl reboot"}