From 43c47560906f80e67ccf093884a50bf3f2972a51 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 7 Aug 2015 05:28:17 +0200 Subject: [PATCH] Add auto update feature You can now keep your system up to date automatically by setting: system.autoUpgrade.enable = true; Fixes #7369. --- .../modules/installer/tools/auto-upgrade.nix | 81 +++++++++++++++++++ nixos/modules/installer/tools/tools.nix | 4 + nixos/modules/module-list.nix | 1 + 3 files changed, 86 insertions(+) create mode 100644 nixos/modules/installer/tools/auto-upgrade.nix diff --git a/nixos/modules/installer/tools/auto-upgrade.nix b/nixos/modules/installer/tools/auto-upgrade.nix new file mode 100644 index 000000000000..b2676b05a02c --- /dev/null +++ b/nixos/modules/installer/tools/auto-upgrade.nix @@ -0,0 +1,81 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let cfg = config.system.autoUpgrade; in + +{ + + options = { + + system.autoUpgrade = { + + enable = mkOption { + type = types.bool; + default = false; + description = '' + Whether to periodically upgrade NixOS to the latest + version. If enabled, a systemd timer will run + nixos-rebuild switch --upgrade once a + day. + ''; + }; + + channel = mkOption { + type = types.nullOr types.str; + default = null; + example = https://nixos.org/channels/nixos-14.12-small; + description = '' + The URI of the NixOS channel to use for automatic + upgrades. By default, this is the channel set using + nix-channel (run nix-channel + --list to see the current value). + ''; + }; + + flags = mkOption { + type = types.listOf types.str; + default = []; + example = [ "-I" "stuff=/home/alice/nixos-stuff" "--option" "extra-binary-caches" "http://my-cache.example.org/" ]; + description = '' + Any additional flags passed to nixos-rebuild. + ''; + }; + + }; + + }; + + config = { + + system.autoUpgrade.flags = + [ "--no-build-output" ] + ++ (if cfg.channel == null + then [ "--upgrade" ] + else [ "-I" "nixpkgs=${cfg.channel}/nixexprs.tar.xz" ]); + + systemd.services.nixos-upgrade = { + description = "NixOS Upgrade"; + + restartIfChanged = false; + unitConfig.X-StopOnRemoval = false; + + serviceConfig.Type = "oneshot"; + + environment = config.nix.envVars // + { inherit (config.environment.sessionVariables) NIX_PATH SSL_CERT_FILE; + HOME = "/root"; + }; + + path = [ pkgs.gnutar pkgs.xz config.nix.package ]; + + script = '' + ${config.system.build.nixos-rebuild}/bin/nixos-rebuild test ${toString cfg.flags} + ''; + + startAt = mkIf cfg.enable "04:40"; + }; + + }; + +} diff --git a/nixos/modules/installer/tools/tools.nix b/nixos/modules/installer/tools/tools.nix index 61744c39d601..04e4c1eb9459 100644 --- a/nixos/modules/installer/tools/tools.nix +++ b/nixos/modules/installer/tools/tools.nix @@ -57,7 +57,9 @@ let in { + config = { + environment.systemPackages = [ nixos-build-vms nixos-install @@ -70,5 +72,7 @@ in system.build = { inherit nixos-install nixos-generate-config nixos-option nixos-rebuild; }; + }; + } diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix index 409f22920871..93d378aa95f8 100644 --- a/nixos/modules/module-list.nix +++ b/nixos/modules/module-list.nix @@ -41,6 +41,7 @@ ./hardware/video/bumblebee.nix ./hardware/video/nvidia.nix ./hardware/video/ati.nix + ./installer/tools/auto-upgrade.nix ./installer/tools/nixos-checkout.nix ./installer/tools/tools.nix ./misc/assertions.nix