mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-07-13 21:50:33 +03:00
nixos/autoUpgrade: add flake support
This commit is contained in:
parent
a448d9156b
commit
c9daa81eff
1 changed files with 69 additions and 29 deletions
|
@ -2,9 +2,9 @@
|
||||||
|
|
||||||
with lib;
|
with lib;
|
||||||
|
|
||||||
let cfg = config.system.autoUpgrade; in
|
let cfg = config.system.autoUpgrade;
|
||||||
|
|
||||||
{
|
in {
|
||||||
|
|
||||||
options = {
|
options = {
|
||||||
|
|
||||||
|
@ -21,6 +21,16 @@ let cfg = config.system.autoUpgrade; in
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
flake = mkOption {
|
||||||
|
type = types.nullOr types.str;
|
||||||
|
default = null;
|
||||||
|
example = "github:kloenk/nix";
|
||||||
|
description = ''
|
||||||
|
The Flake URI of the NixOS configuration to build.
|
||||||
|
Disables the option <option>system.autoUpgrade.channel</option>.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
channel = mkOption {
|
channel = mkOption {
|
||||||
type = types.nullOr types.str;
|
type = types.nullOr types.str;
|
||||||
default = null;
|
default = null;
|
||||||
|
@ -35,10 +45,20 @@ let cfg = config.system.autoUpgrade; in
|
||||||
|
|
||||||
flags = mkOption {
|
flags = mkOption {
|
||||||
type = types.listOf types.str;
|
type = types.listOf types.str;
|
||||||
default = [];
|
default = [ ];
|
||||||
example = [ "-I" "stuff=/home/alice/nixos-stuff" "--option" "extra-binary-caches" "http://my-cache.example.org/" ];
|
example = [
|
||||||
|
"-I"
|
||||||
|
"stuff=/home/alice/nixos-stuff"
|
||||||
|
"--option"
|
||||||
|
"extra-binary-caches"
|
||||||
|
"http://my-cache.example.org/"
|
||||||
|
];
|
||||||
description = ''
|
description = ''
|
||||||
Any additional flags passed to <command>nixos-rebuild</command>.
|
Any additional flags passed to <command>nixos-rebuild</command>.
|
||||||
|
|
||||||
|
If you are using flakes and use a local repo you can add
|
||||||
|
<command>[ "--update-input" "nixpkgs" "--commit-lock-file" ]</command>
|
||||||
|
to update nixpkgs.
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -82,11 +102,23 @@ let cfg = config.system.autoUpgrade; in
|
||||||
|
|
||||||
config = lib.mkIf cfg.enable {
|
config = lib.mkIf cfg.enable {
|
||||||
|
|
||||||
system.autoUpgrade.flags =
|
assertions = [{
|
||||||
[ "--no-build-output" ]
|
assertion = !((cfg.channel != null) && (cfg.flake != null));
|
||||||
++ (if cfg.channel == null
|
message = ''
|
||||||
then [ "--upgrade" ]
|
The options 'system.autoUpgrade.channels' and 'system.autoUpgrade.flake' cannot both be set.
|
||||||
else [ "-I" "nixpkgs=${cfg.channel}/nixexprs.tar.xz" ]);
|
'';
|
||||||
|
}];
|
||||||
|
|
||||||
|
system.autoUpgrade.flags = [ "--no-build-output" ]
|
||||||
|
++ (if cfg.flake == null then
|
||||||
|
(if cfg.channel == null then
|
||||||
|
[ "--upgrade" ]
|
||||||
|
else [
|
||||||
|
"-I"
|
||||||
|
"nixpkgs=${cfg.channel}/nixexprs.tar.xz"
|
||||||
|
])
|
||||||
|
else
|
||||||
|
[ "--flake ${cfg.flake}" ]);
|
||||||
|
|
||||||
systemd.services.nixos-upgrade = {
|
systemd.services.nixos-upgrade = {
|
||||||
description = "NixOS Upgrade";
|
description = "NixOS Upgrade";
|
||||||
|
@ -96,33 +128,41 @@ let cfg = config.system.autoUpgrade; in
|
||||||
|
|
||||||
serviceConfig.Type = "oneshot";
|
serviceConfig.Type = "oneshot";
|
||||||
|
|
||||||
environment = config.nix.envVars //
|
environment = config.nix.envVars // {
|
||||||
{ inherit (config.environment.sessionVariables) NIX_PATH;
|
inherit (config.environment.sessionVariables) NIX_PATH;
|
||||||
HOME = "/root";
|
HOME = "/root";
|
||||||
} // config.networking.proxy.envVars;
|
} // config.networking.proxy.envVars;
|
||||||
|
|
||||||
path = with pkgs; [ coreutils gnutar xz.bin gzip gitMinimal config.nix.package.out ];
|
path = with pkgs; [
|
||||||
|
coreutils
|
||||||
|
gnutar
|
||||||
|
xz.bin
|
||||||
|
gzip
|
||||||
|
gitMinimal
|
||||||
|
config.nix.package.out
|
||||||
|
];
|
||||||
|
|
||||||
script = let
|
script = let
|
||||||
nixos-rebuild = "${config.system.build.nixos-rebuild}/bin/nixos-rebuild";
|
nixos-rebuild =
|
||||||
in
|
"${config.system.build.nixos-rebuild}/bin/nixos-rebuild";
|
||||||
if cfg.allowReboot then ''
|
in if cfg.allowReboot then ''
|
||||||
${nixos-rebuild} boot ${toString cfg.flags}
|
${nixos-rebuild} boot ${toString cfg.flags}
|
||||||
booted="$(readlink /run/booted-system/{initrd,kernel,kernel-modules})"
|
booted="$(readlink /run/booted-system/{initrd,kernel,kernel-modules})"
|
||||||
built="$(readlink /nix/var/nix/profiles/system/{initrd,kernel,kernel-modules})"
|
built="$(readlink /nix/var/nix/profiles/system/{initrd,kernel,kernel-modules})"
|
||||||
if [ "$booted" = "$built" ]; then
|
if [ "$booted" = "$built" ]; then
|
||||||
${nixos-rebuild} switch ${toString cfg.flags}
|
${nixos-rebuild} switch ${toString cfg.flags}
|
||||||
else
|
else
|
||||||
/run/current-system/sw/bin/shutdown -r +1
|
/run/current-system/sw/bin/shutdown -r +1
|
||||||
fi
|
fi
|
||||||
'' else ''
|
'' else ''
|
||||||
${nixos-rebuild} switch ${toString cfg.flags}
|
${nixos-rebuild} switch ${toString cfg.flags}
|
||||||
'';
|
'';
|
||||||
|
|
||||||
startAt = cfg.dates;
|
startAt = cfg.dates;
|
||||||
};
|
};
|
||||||
|
|
||||||
systemd.timers.nixos-upgrade.timerConfig.RandomizedDelaySec = cfg.randomizedDelaySec;
|
systemd.timers.nixos-upgrade.timerConfig.RandomizedDelaySec =
|
||||||
|
cfg.randomizedDelaySec;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue