@ -80,6 +80,7 @@ in {
Reboot the system into the new generation instead of a switch
if the new generation uses a different kernel , kernel modules
or initrd than the booted system .
See <option> rebootWindow < /option > for configuring the times at which a reboot is allowed .
'' ;
} ;
@ -96,6 +97,32 @@ in {
'' ;
} ;
rebootWindow = mkOption {
description = ''
Define a lower and upper time value ( in HH:MM format ) which
constitute a time window during which reboots are allowed after an upgrade .
This option only has an effect when <option> allowReboot < /option > is enabled .
The default value of <literal> null < /literal > means that reboots are allowed at any time .
'' ;
default = null ;
example = { lower = " 0 1 : 0 0 " ; upper = " 0 5 : 0 0 " ; } ;
type = with types ; nullOr ( submodule {
options = {
lower = mkOption {
description = " L o w e r l i m i t o f t h e r e b o o t w i n d o w " ;
type = types . strMatching " [ [ : d i g i t : ] ] { 2 } : [ [ : d i g i t : ] ] { 2 } " ;
example = " 0 1 : 0 0 " ;
} ;
upper = mkOption {
description = " U p p e r l i m i t o f t h e r e b o o t w i n d o w " ;
type = types . strMatching " [ [ : d i g i t : ] ] { 2 } : [ [ : d i g i t : ] ] { 2 } " ;
example = " 0 5 : 0 0 " ;
} ;
} ;
} ) ;
} ;
} ;
} ;
@ -110,12 +137,10 @@ in {
} ] ;
system . autoUpgrade . flags = ( if cfg . flake == null then
[ " - - n o - b u i l d - o u t p u t " ] ++ ( if cfg . channel == null then
[ " - - u p g r a d e " ]
else [
[ " - - n o - b u i l d - o u t p u t " ] ++ optionals ( cfg . channel != null ) [
" - I "
" n i x p k g s = ${ cfg . channel } / n i x e x p r s . t a r . x z "
] )
]
else
[ " - - f l a k e ${ cfg . flake } " ] ) ;
@ -143,19 +168,52 @@ in {
] ;
script = let
nixos-rebuild =
" ${ config . system . build . nixos-rebuild } / b i n / n i x o s - r e b u i l d " ;
nixos-rebuild = " ${ config . system . build . nixos-rebuild } / b i n / n i x o s - r e b u i l d " ;
date = " ${ pkgs . coreutils } / b i n / d a t e " ;
readlink = " ${ pkgs . coreutils } / b i n / r e a d l i n k " ;
shutdown = " ${ pkgs . systemd } / b i n / s h u t d o w n " ;
upgradeFlag = optional ( cfg . channel == null ) " - - u p g r a d e " ;
in if cfg . allowReboot then ''
$ { nixos-rebuild } boot $ { toString cfg . flags }
booted = " $ ( r e a d l i n k / r u n / b o o t e d - s y s t e m / { i n i t r d , k e r n e l , k e r n e l - m o d u l e s } ) "
built = " $ ( r e a d l i n k / n i x / v a r / n i x / p r o f i l e s / s y s t e m / { i n i t r d , k e r n e l , k e r n e l - m o d u l e s } ) "
if [ " $ b o o t e d " = " $ b u i l t " ] ; then
$ { nixos-rebuild } boot $ { toString ( cfg . flags ++ upgradeFlag ) }
booted = " $ ( ${ readlink } / r u n / b o o t e d - s y s t e m / { i n i t r d , k e r n e l , k e r n e l - m o d u l e s } ) "
built = " $ ( ${ readlink } / n i x / v a r / n i x / p r o f i l e s / s y s t e m / { i n i t r d , k e r n e l , k e r n e l - m o d u l e s } ) "
$ { optionalString ( cfg . rebootWindow != null ) ''
current_time = " $ ( ${ date } + % H : % M ) "
lower = " ${ cfg . rebootWindow . lower } "
upper = " ${ cfg . rebootWindow . upper } "
if [ [ " ' ' ${ lower } " < " ' ' ${ upper } " ] ] ; then
if [ [ " ' ' ${ current_time } " > " ' ' ${ lower } " ] ] && \
[ [ " ' ' ${ current_time } " < " ' ' ${ upper } " ] ] ; then
do_reboot = " t r u e "
else
do_reboot = " f a l s e "
fi
else
# lower > upper, so we are crossing midnight (e.g. lower=23h, upper=6h)
# we want to reboot if cur > 23h or cur < 6h
if [ [ " ' ' ${ current_time } " < " ' ' ${ upper } " ] ] || \
[ [ " ' ' ${ current_time } " > " ' ' ${ lower } " ] ] ; then
do_reboot = " t r u e "
else
do_reboot = " f a l s e "
fi
fi
'' }
if [ " ' ' ${ booted } " = " ' ' ${ built } " ] ; then
$ { nixos-rebuild } switch $ { toString cfg . flags }
$ { optionalString ( cfg . rebootWindow != null ) ''
elif [ " ' ' ${ do_reboot } " != true ] ; then
echo " O u t s i d e o f c o n f i g u r e d r e b o o t w i n d o w , s k i p p i n g . "
'' }
else
/run/current-system/sw/bin/shutdown - r + 1
$ { shutdown } - r + 1
fi
'' e l s e ''
$ { nixos-rebuild } switch $ { toString cfg . flags }
$ { nixos-rebuild } switch $ { toString ( cfg . flags ++ upgradeFlag ) }
'' ;
startAt = cfg . dates ;
@ -167,3 +225,4 @@ in {
} ;
}