2022-01-03 10:50:50 +00:00
{
description = " N i x i n a t e y o u r s y s t e m s 🕶 ️ " ;
2022-01-26 20:17:20 +00:00
inputs = {
2022-02-07 00:59:29 +00:00
nixpkgs . url = " g i t h u b : n i x o s / n i x p k g s / n i x o s - u n s t a b l e " ;
2022-01-26 20:17:20 +00:00
examples . url = " p a t h : . / e x a m p l e s " ;
} ;
outputs = { self , nixpkgs , examples , . . . }:
2022-01-03 10:50:50 +00:00
let
version = builtins . substring 0 8 self . lastModifiedDate ;
supportedSystems = [ " x 8 6 _ 6 4 - l i n u x " " x 8 6 _ 6 4 - d a r w i n " " a a r c h 6 4 - l i n u x " " a a r c h 6 4 - d a r w i n " ] ;
forAllSystems = nixpkgs . lib . genAttrs supportedSystems ;
nixpkgsFor = forAllSystems ( system : import nixpkgs { inherit system ; overlays = [ self . overlay ] ; } ) ;
in rec
2022-02-15 18:19:48 -08:00
{
2022-01-03 10:50:50 +00:00
overlay = final : prev : {
generateApps = flake :
let
machines = builtins . attrNames flake . nixosConfigurations ;
validMachines = final . lib . remove " " ( final . lib . forEach machines ( x : final . lib . optionalString ( flake . nixosConfigurations . " ${ x } " . _module . args ? nixinate ) " ${ x } " ) ) ;
2022-02-15 18:42:53 -08:00
mkDeployScript = { machine , dryRun }: let
2022-02-15 18:37:50 -08:00
inherit ( builtins ) abort ;
n = flake . nixosConfigurations . ${ machine } . _module . args . nixinate ;
2022-02-15 18:39:10 -08:00
user = n . sshUser or " r o o t " ;
2022-02-15 18:37:50 -08:00
host = n . host ;
where = n . buildOn or " r e m o t e " ;
remote = if where = = " r e m o t e " then true else if where = = " l o c a l " then false else abort " _ m o d u l e . a r g s . n i x i n a t e . b u i l d O n i s n o t s e t t o a v a l i d v a l u e o f ' l o c a l ' o r ' r e m o t e ' " ;
2022-02-15 18:42:53 -08:00
switch = if dryRun then " d r y - a c t i v a t e " else " s w i t c h " ;
2022-02-23 12:42:56 -08:00
rollbackScript = let
inherit ( builtins ) toString ;
inherit ( final . lib . strings ) optionalString ;
r = n . rollback or { } ;
enabled = r . enabled or true ;
init = r . init or 500 ;
limit = r . limit or 8 ;
timeout = r . timeout or 10 ;
in optionalString enabled ''
rollbackAccumulator = $ { toString limit }
exponent = 0
until $ { final . openssh } /bin/ssh - o ConnectTimeout = $ { toString timeout } - t $ { user } @ $ { host } ' sudo rm /tmp/.nixinate-deploy-success ' ; do
rollbackWait = $ ( ( $ { toString init } * ( 2 * * exponent ++ ) ) )
echo " C o u l d n o t a c c e s s ${ machine } , t r y i n g a g a i n i n $ r o l l b a c k W a i t m i l l i s e c o n d s . " & > 2
sleep $ ( ( rollbackWait / 1000 ) )
if [ [ $ ( ( - - rollbackAccumulator ) ) = = 0 ] ] ; # --rollbackAccumulator may appear as a flag, however it's inside of $(()), so it decrements the value and yields it.
echo " C a n n o t a c c e s s ${ machine } . R o l l b a c k w i l l h a p p e n . " & > 2
exit 1
] ] ;
done
'' ;
2022-02-15 18:37:50 -08:00
script = ''
set - e
echo " 🚀 D e p l o y i n g n i x o s C o n f i g u r a t i o n s . ${ machine } f r o m ${ flake } "
echo " 👤 S S H U s e r : ${ user } "
echo " 🌐 S S H H o s t : ${ host } "
'' + ( i f r e m o t e t h e n ''
2022-01-26 22:19:36 +00:00
echo " 🚀 S e n d i n g f l a k e t o ${ machine } v i a r s y n c : "
2022-02-15 18:37:50 -08:00
( set - x ; $ { final . rsync } /bin/rsync - q - vz - - recursive - - zc = zstd $ { flake } /* $ { u s e r } @ $ { h o s t } : / t m p / n i x c f g / )
2022-01-26 22:19:36 +00:00
echo " 🤞 A c t i v a t i n g c o n f i g u r a t i o n o n ${ machine } v i a s s h : "
2022-02-15 18:42:53 -08:00
( set - x ; $ { final . openssh } /bin/ssh - t $ { user } @ $ { host } ' sudo nixos-rebuild $ { switch } - - flake /tmp/nixcfg #${machine}' )
2022-02-15 18:37:50 -08:00
'' e l s e ''
2022-02-07 00:58:06 +00:00
echo " 🔨 B u i l d i n g s y s t e m c l o s u r e l o c a l l y , c o p y i n g i t t o r e m o t e s t o r e a n d a c t i v a t i n g i t : "
2022-02-15 18:42:53 -08:00
( set - x ; NIX_SSHOPTS = " - t " $ { final . nixos-rebuild } /bin/nixos-rebuild $ { switch } - - flake $ { flake } #${machine} --target-host ${user}@${host} --use-remote-sudo )
2022-02-23 12:42:56 -08:00
'' ) + r o l l b a c k S c r i p t + ''
echo " ${ machine } h a s f i n i s h e d d e p l o y i n g . "
'' ;
2022-02-15 18:37:50 -08:00
in final . writeScript " d e p l o y - ${ machine } . s h " script ;
2022-01-03 10:50:50 +00:00
in
{
nixinate =
(
nixpkgs . lib . genAttrs
validMachines
2022-02-15 18:19:48 -08:00
( x :
{
2022-01-03 10:50:50 +00:00
type = " a p p " ;
2022-02-15 18:42:53 -08:00
program = toString ( mkDeployScript {
machine = x ;
dryRun = false ;
} ) ;
2022-01-03 10:50:50 +00:00
}
)
2022-02-15 18:42:53 -08:00
// nixpkgs . lib . genAttrs
( map ( a : a + " - d r y - r u n " ) validMachines )
( x :
{
type = " a p p " ;
program = toString ( mkDeployScript {
machine = x ;
dryRun = true ;
} ) ;
}
)
2022-01-03 10:50:50 +00:00
) ;
} ;
} ;
nixinate = forAllSystems ( system : nixpkgsFor . ${ system } . generateApps ) ;
2022-01-26 20:17:20 +00:00
apps = nixinate . x86_64-linux examples ;
2022-01-03 10:50:50 +00:00
} ;
2022-02-15 18:19:48 -08:00
}