1 { config, lib, pkgs, ... }:
3 let cfg = config.system.autoUpgradeWithPinch;
6 system.autoUpgradeWithPinch = {
12 Whether to periodically upgrade NixOS to the latest version.
13 Presumes that /etc/nixos is a git repo with a remote and
14 contains a pinch file called "channels".
22 Specification (in the format described by
23 <citerefentry><refentrytitle>systemd.time</refentrytitle>
24 <manvolnum>7</manvolnum></citerefentry>) of the time at
25 which the update will occur.
32 GPG key that signs updates. Updates are only merged if the commit
33 at the tip of the remote branch is signed with this key.
39 config = lib.mkIf cfg.enable {
41 (import ../overlays/keyedgit.nix)
42 (import ../overlays/pinch.nix)
44 auto-upgrade = super.writeShellScriptBin "auto-upgrade" ''
45 flock /run/auto-upgrade-with-pinch ${super.writeShellScript "auto-upgrade-with-lock-held" ''
49 ${self.keyedgit cfg.key}/bin/git pull --ff-only --verify-signatures
50 ${self.pinch}/bin/pinch update channels
53 ${config.system.build.nixos-rebuild}/bin/nixos-rebuild switch --no-build-output
59 environment.systemPackages = [ pkgs.auto-upgrade ];
61 systemd.services.nixos-upgrade = {
62 description = "NixOS Upgrade";
63 restartIfChanged = false;
64 unitConfig.X-StopOnRemoval = false;
65 serviceConfig.Type = "oneshot";
66 environment = config.nix.envVars // {
67 inherit (config.environment.sessionVariables) NIX_PATH;
69 } // config.networking.proxy.envVars;
72 config.nix.package.out
84 # Chill for awhile before applying updates. If applying an update
85 # badly breaks things, we want a window in which an operator can
86 # intervene either to fix the problem or disable automatic updates.
89 # Wait until outside business hours
91 day_of_week=$(date +%u)
92 business_start=$(date -d 8:00 +%s)
93 business_end=$( date -d 17:00 +%s)
94 if (( day_of_week <= 5 && now > business_start && now < business_end ));then
95 delay=$((business_end - now))
96 echo "Waiting $delay seconds so we don't upgrade during business hours" >&2
100 ${pkgs.auto-upgrade}/bin/auto-upgrade