nixinate/flake.nix
matthewcroughan fcae9a11d0 Provide pkgs in forAllSystems
This is stolen from DavHau's dream2nix flake which reworks forAllSystems to provide two arguments, the pkgs and the system.
2022-03-31 17:40:00 +01:00

77 lines
3.5 KiB
Nix
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
description = "Nixinate your systems 🕶";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
};
outputs = { self, nixpkgs, ... }:
let
version = builtins.substring 0 8 self.lastModifiedDate;
supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
forSystems = systems: f:
nixpkgs.lib.genAttrs systems
(system: f system nixpkgs.legacyPackages.${system});
forAllSystems = forSystems supportedSystems;
nixpkgsFor = forAllSystems (system: pkgs: import nixpkgs { inherit system; overlays = [ self.overlay ]; });
in rec
{
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}" ));
mkDeployScript = { machine, dryRun }: let
inherit (builtins) abort;
n = flake.nixosConfigurations.${machine}._module.args.nixinate;
user = n.sshUser or "root";
host = n.host;
where = n.buildOn or "remote";
remote = if where == "remote" then true else if where == "local" then false else abort "_module.args.nixinate.buildOn is not set to a valid value of 'local' or 'remote'";
switch = if dryRun then "dry-activate" else "switch";
script = ''
set -e
echo "🚀 Deploying nixosConfigurations.${machine} from ${flake}"
echo "👤 SSH User: ${user}"
echo "🌐 SSH Host: ${host}"
'' + (if remote then ''
echo "🚀 Sending flake to ${machine} via nix copy:"
( set -x; ${final.nix}/bin/nix copy ${flake} --to ssh://${user}@${host} )
echo "🤞 Activating configuration on ${machine} via ssh:"
( set -x; ${final.openssh}/bin/ssh -t ${user}@${host} 'sudo nixos-rebuild ${switch} --flake ${flake}#${machine}' )
'' else ''
echo "🔨 Building system closure locally, copying it to remote store and activating it:"
( set -x; NIX_SSHOPTS="-t" ${final.nixos-rebuild}/bin/nixos-rebuild ${switch} --flake ${flake}#${machine} --target-host ${user}@${host} --use-remote-sudo )
'');
in final.writeScript "deploy-${machine}.sh" script;
in
{
nixinate =
(
nixpkgs.lib.genAttrs
validMachines
(x:
{
type = "app";
program = toString (mkDeployScript {
machine = x;
dryRun = false;
});
}
)
// nixpkgs.lib.genAttrs
(map (a: a + "-dry-run") validMachines)
(x:
{
type = "app";
program = toString (mkDeployScript {
machine = nixpkgs.lib.removeSuffix "-dry-run" x;
dryRun = true;
});
}
)
);
};
};
nixinate = forAllSystems (system: pkgs: nixpkgsFor.${system}.generateApps);
};
}