nixos-configuration/flake.nix
2023-06-20 11:28:58 +00:00

153 lines
4.3 KiB
Nix

{
inputs = {
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:nixos/nixpkgs/nixos-23.05";
nixpkgsMaster.url = "github:NixOS/nixpkgs/master";
nur.url = "github:nix-community/NUR";
home-manager = {
url = "github:nix-community/home-manager/release-23.05";
inputs.nixpkgs.follows = "nixpkgs";
};
nix-pre-commit-hooks = {
url = "github:cachix/pre-commit-hooks.nix/master";
inputs.flake-utils.follows = "flake-utils";
};
sops-nix = {
url = "github:Mic92/sops-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
krops = {
url = "github:Mic92/krops";
inputs = {
flake-utils.follows = "flake-utils";
nixpkgs.follows = "nixpkgs";
};
};
};
outputs =
{ self
, nixpkgs
, flake-utils
, krops
, home-manager
, nur
, nix-pre-commit-hooks
, ...
}@inputs: flake-utils.lib.eachSystem [
"x86_64-linux"
]
(system:
let
pkgs = import nixpkgs { inherit system; };
inherit (pkgs) lib;
in
{
checks = {
pre-commit-check = nix-pre-commit-hooks.lib.${system}.run {
src = self;
hooks = {
black.enable = true;
nixpkgs-fmt.enable = true;
shellcheck.enable = true;
};
};
};
devShells.default = pkgs.mkShell {
buildInputs = (with pkgs; [
black
just
nixpkgs-fmt
shellcheck
sops
ssh-to-age
]);
shellHook = ''
${self.checks.${system}.pre-commit-check.shellHook}
'';
};
apps = lib.mapAttrs
(name: program: { type = "app"; program = toString program; })
(flake-utils.lib.flattenTree {
deploy = lib.recurseIntoAttrs (lib.mapAttrs
(hostname: machine:
let
inherit (krops.packages.${system}) writeCommand;
inherit (krops) lib;
in
writeCommand "deploy-${hostname}" {
target = lib.mkTarget "root@${machine.config.deployment.targetHost}" // {
extraOptions = [
# force allocation of tty to allow aborting with ^C and to show build progress
"-t"
];
};
source = lib.evalSource (lib.singleton {
config.file = {
path = toString ./.;
useChecksum = true;
};
});
command = targetPath: ''
nixos-rebuild switch --flake ${targetPath}/config -L --keep-going
'';
}
)
self.nixosConfigurations);
showKeyFingerprint = pkgs.writeShellScript "show-key-fingerprint" ''
${pkgs.gnupg}/bin/gpg --with-fingerprint --with-colons --show-key "keys/''${1}.asc" | awk -F: '$1 == "fpr" { print $10; exit }'
'';
});
}) // {
overlay = import ./pkgs;
nixosConfigurations = nixpkgs.lib.mapAttrs
(hostname: { system
, extraModules ? [ ]
, targetHost ? hostname
, nixpkgs ? inputs.nixpkgs
}: nixpkgs.lib.nixosSystem rec {
inherit system;
specialArgs = { inherit self system; };
modules = [
(./hosts + "/${hostname}/configuration.nix")
./modules
{
_module.args.inputs = inputs;
}
# deployment settings
({ lib, ... }: {
options.deployment = {
targetHost = lib.mkOption {
type = lib.types.str;
readOnly = true;
internal = true;
};
};
config.deployment = {
inherit targetHost;
};
})
] ++ [{
nixpkgs.overlays = [ nur.overlay ];
}] ++ [
home-manager.nixosModules.home-manager
] ++ (with inputs; [
sops-nix.nixosModules.sops
]) ++ extraModules;
})
(import ./hosts inputs);
};
}