nix-auto-update.nix (2281B)
1 { config, lib, pkgs, ... }: 2 3 with lib; 4 let 5 cfg = config.profiles.nix-auto-update; 6 in 7 { 8 options = { 9 profiles.nix-auto-update = { 10 enable = mkOption { 11 default = false; 12 description = "Enable nix-auto-update profile"; 13 type = types.bool; 14 }; 15 autoUpgrade = mkOption { 16 default = true; 17 description = "Automatically try to upgrade the system"; 18 type = types.bool; 19 }; 20 dates = mkOption { 21 default = "weekly"; 22 description = "Specification (in the format described by systemd.time(7)) of the time at which the auto-update will run. "; 23 type = types.str; 24 }; 25 version = mkOption { 26 default = "20.03"; 27 description = "System version (NixOS)"; 28 type = types.str; 29 }; 30 }; 31 }; 32 config = mkIf cfg.enable (mkMerge [ 33 { 34 system = { 35 stateVersion = cfg.version; 36 }; 37 } 38 ( 39 mkIf cfg.autoUpgrade { 40 systemd.services.nixos-update = { 41 description = "NixOS Upgrade"; 42 unitConfig.X-StopOnRemoval = false; 43 restartIfChanged = false; 44 serviceConfig.Type = "oneshot"; 45 environment = config.nix.envVars 46 // { 47 inherit (config.environment.sessionVariables) NIX_PATH; 48 HOME = "/root"; 49 }; 50 script = '' 51 export PATH=/run/current-system/sw/bin 52 cd /etc/nixos/ 53 make switch 54 ''; 55 startAt = cfg.dates; 56 onFailure = [ "status-email-root@%n.service" ]; 57 }; 58 systemd.services.etc-nixos-git-update = { 59 description = "Update NixOS source git repository"; 60 unitConfig.X-StopOnRemoval = false; 61 restartIfChanged = false; 62 serviceConfig.Type = "oneshot"; 63 serviceConfig.User = "vincent"; 64 environment = config.nix.envVars 65 // { 66 inherit (config.environment.sessionVariables) NIX_PATH; 67 }; 68 script = '' 69 export PATH=/run/current-system/sw/bin 70 cd /etc/nixos/ 71 git pull --rebase --autostash --recurse-submodules 72 ''; 73 startAt = "daily"; 74 onFailure = [ "status-email-root@%n.service" ]; 75 }; 76 } 77 ) 78 ]); 79 }