2024-01-06 01:19:35 +01:00
|
|
|
# SPDX-FileCopyrightText: 2021-2024 Simon Bruder <simon@sbruder.de>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
2021-05-01 16:30:48 +02:00
|
|
|
{
|
|
|
|
description = "NixOS system configuration";
|
|
|
|
|
|
|
|
inputs = {
|
|
|
|
flake-utils.url = "github:numtide/flake-utils";
|
|
|
|
|
2024-06-02 11:44:41 +02:00
|
|
|
nixpkgs.url = "github:nixos/nixpkgs/nixos-24.05";
|
2021-05-01 16:30:48 +02:00
|
|
|
nixpkgs-unstable.url = "github:nixos/nixpkgs/nixos-unstable";
|
|
|
|
|
2024-06-02 11:44:41 +02:00
|
|
|
home-manager.url = "github:nix-community/home-manager/release-24.05";
|
2021-05-01 16:30:48 +02:00
|
|
|
home-manager.inputs.nixpkgs.follows = "nixpkgs";
|
2023-04-16 11:26:09 +02:00
|
|
|
home-manager-unstable.url = "github:nix-community/home-manager";
|
|
|
|
home-manager-unstable.inputs.nixpkgs.follows = "nixpkgs-unstable";
|
2021-05-01 16:30:48 +02:00
|
|
|
|
|
|
|
krops.url = "github:Mic92/krops";
|
|
|
|
krops.inputs.flake-utils.follows = "flake-utils";
|
|
|
|
krops.inputs.nixpkgs.follows = "nixpkgs";
|
|
|
|
|
|
|
|
nixos-hardware.url = "github:nixos/nixos-hardware/master";
|
|
|
|
|
|
|
|
nix-pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix/master";
|
2022-03-06 09:22:04 +01:00
|
|
|
nix-pre-commit-hooks.inputs.nixpkgs.follows = "nixpkgs-unstable";
|
2021-05-01 16:30:48 +02:00
|
|
|
|
|
|
|
sops-nix.url = "github:Mic92/sops-nix";
|
|
|
|
sops-nix.inputs.nixpkgs.follows = "nixpkgs";
|
|
|
|
|
2022-05-31 14:31:45 +02:00
|
|
|
infinisilSystem.url = "github:Infinisil/system/9f7cf7497d26d57996edc7f70edcf7d2cccfdf60";
|
2021-05-01 16:30:48 +02:00
|
|
|
infinisilSystem.flake = false;
|
2021-05-02 09:44:01 +02:00
|
|
|
|
2021-05-13 13:42:36 +02:00
|
|
|
nixpkgs-overlay.url = "git+https://git.sbruder.de/simon/nixpkgs-overlay";
|
|
|
|
nixpkgs-overlay.inputs.flake-utils.follows = "flake-utils";
|
|
|
|
nixpkgs-overlay.inputs.nixpkgs.follows = "nixpkgs";
|
|
|
|
nixpkgs-overlay.inputs.nix-pre-commit-hooks.follows = "nix-pre-commit-hooks";
|
|
|
|
|
2021-05-02 09:44:01 +02:00
|
|
|
bang-evaluator.url = "git+https://git.sbruder.de/simon/bangs";
|
|
|
|
bang-evaluator.inputs.flake-utils.follows = "flake-utils";
|
|
|
|
bang-evaluator.inputs.nixpkgs.follows = "nixpkgs";
|
2022-07-16 19:52:40 +02:00
|
|
|
|
|
|
|
password-hash-self-service.url = "git+https://git.sbruder.de/simon/password-hash-self-service";
|
|
|
|
password-hash-self-service.inputs.flake-utils.follows = "flake-utils";
|
2021-05-01 16:30:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
outputs =
|
|
|
|
{ self
|
|
|
|
, flake-utils
|
|
|
|
, krops
|
|
|
|
, nix-pre-commit-hooks
|
|
|
|
, nixpkgs
|
2021-08-28 13:53:38 +02:00
|
|
|
, nixpkgs-overlay
|
2021-05-15 10:04:44 +02:00
|
|
|
, ...
|
|
|
|
}@inputs: flake-utils.lib.eachDefaultSystem
|
2021-05-01 16:30:48 +02:00
|
|
|
(system:
|
|
|
|
let
|
2021-11-07 20:31:48 +01:00
|
|
|
pkgs = import nixpkgs { inherit system; };
|
2021-05-01 16:30:48 +02:00
|
|
|
inherit (pkgs) lib;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
checks = {
|
|
|
|
pre-commit-check = nix-pre-commit-hooks.lib.${system}.run {
|
2021-11-07 20:31:48 +01:00
|
|
|
src = self;
|
2021-05-01 16:30:48 +02:00
|
|
|
hooks = {
|
2021-06-01 00:02:27 +02:00
|
|
|
black.enable = true;
|
2021-05-01 16:30:48 +02:00
|
|
|
nixpkgs-fmt.enable = true;
|
2021-05-31 23:59:13 +02:00
|
|
|
shellcheck.enable = true;
|
2024-01-06 01:19:35 +01:00
|
|
|
|
|
|
|
reuse = {
|
|
|
|
enable = true;
|
|
|
|
name = "reuse";
|
|
|
|
entry = "reuse lint";
|
|
|
|
pass_filenames = false;
|
|
|
|
};
|
2021-05-01 16:30:48 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-05-02 18:07:51 +02:00
|
|
|
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"
|
2021-05-01 16:30:48 +02:00
|
|
|
];
|
|
|
|
};
|
2021-05-02 18:07:51 +02:00
|
|
|
source = lib.evalSource (lib.singleton {
|
|
|
|
config.file = {
|
2021-11-07 20:31:48 +01:00
|
|
|
path = toString self;
|
2021-05-02 18:07:51 +02:00
|
|
|
useChecksum = true;
|
2023-04-15 18:09:40 +02:00
|
|
|
filters = [
|
|
|
|
{
|
|
|
|
type = "include";
|
|
|
|
pattern = "/machines/${hostname}/";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
type = "exclude";
|
|
|
|
pattern = "/machines/*/";
|
|
|
|
}
|
|
|
|
];
|
2021-05-02 18:07:51 +02:00
|
|
|
};
|
|
|
|
});
|
|
|
|
command = targetPath: ''
|
|
|
|
nixos-rebuild switch --flake ${targetPath}/config -L --keep-going
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
)
|
|
|
|
self.nixosConfigurations);
|
|
|
|
|
2021-12-26 12:54:05 +01:00
|
|
|
deploy-local = lib.recurseIntoAttrs (lib.mapAttrs
|
|
|
|
(hostname: machine: pkgs.writeShellScript "deploy-local-${hostname}" ''
|
2023-05-31 17:50:51 +02:00
|
|
|
set -euo pipefail
|
|
|
|
|
2023-06-01 16:50:57 +02:00
|
|
|
SUBSTITUTE="-s"
|
|
|
|
for i in "$@"; do
|
|
|
|
case $i in
|
|
|
|
-S)
|
|
|
|
SUBSTITUTE=""
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2023-05-31 17:50:51 +02:00
|
|
|
closure="$(nix build -L --print-out-paths .#nixosConfigurations.${hostname}.config.system.build.toplevel)"
|
|
|
|
nix copy \
|
2023-06-01 16:50:57 +02:00
|
|
|
$SUBSTITUTE \
|
2023-05-31 17:50:51 +02:00
|
|
|
-L \
|
|
|
|
--to ssh://root@${machine.config.deployment.targetHost} \
|
|
|
|
"$closure"
|
|
|
|
ssh root@${machine.config.deployment.targetHost} nix-env -p /nix/var/nix/profiles/system --set ''${closure}
|
|
|
|
ssh root@${machine.config.deployment.targetHost} ''${closure}/bin/switch-to-configuration switch
|
|
|
|
return="$?"
|
|
|
|
if [ "$return" != 0 ]; then
|
|
|
|
echo "Error(s) occurred while Switching configuration"
|
|
|
|
exit "$retuen"
|
|
|
|
fi
|
2021-12-26 12:54:05 +01:00
|
|
|
'')
|
|
|
|
self.nixosConfigurations);
|
|
|
|
|
2021-05-02 18:07:51 +02:00
|
|
|
unlock = lib.recurseIntoAttrs (lib.mapAttrs
|
|
|
|
(hostname: machine:
|
|
|
|
let
|
|
|
|
inherit (machine.config.deployment)
|
|
|
|
targetHost
|
|
|
|
unlockOverV4;
|
|
|
|
in
|
|
|
|
pkgs.writeShellScript "unlock-${hostname}" ''
|
|
|
|
set -exo pipefail
|
|
|
|
# opening luks fails if gpg-agent is not unlocked yet
|
2024-05-11 23:45:51 +02:00
|
|
|
pass "devices/${hostname}/luks" | ssh \
|
2021-05-02 18:07:51 +02:00
|
|
|
${lib.optionalString unlockOverV4 "-4"} \
|
|
|
|
-p 2222 \
|
2022-10-03 20:19:42 +02:00
|
|
|
"root@${targetHost}" \
|
2024-05-11 23:45:51 +02:00
|
|
|
"cat > /crypt-ramfs/passphrase"
|
2021-05-02 18:07:51 +02:00
|
|
|
'')
|
|
|
|
self.nixosConfigurations);
|
|
|
|
|
|
|
|
showKeyFingerprint = pkgs.writeShellScript "show-key-fingerprint" ''
|
|
|
|
gpg --with-fingerprint --with-colons --show-key "keys/''${1}.asc" | awk -F: '$1 == "fpr" { print $10; exit }'
|
|
|
|
'';
|
|
|
|
|
|
|
|
});
|
2021-05-01 16:30:48 +02:00
|
|
|
|
2024-08-20 23:04:38 +02:00
|
|
|
packages = {
|
|
|
|
kexec-bundle = (nixpkgs.lib.nixosSystem {
|
|
|
|
inherit system;
|
|
|
|
|
|
|
|
modules = [
|
|
|
|
./modules/pubkeys.nix
|
|
|
|
./modules/ssh.nix
|
|
|
|
|
|
|
|
({ modulesPath, ... }: {
|
|
|
|
imports = [
|
|
|
|
(modulesPath + "/installer/netboot/netboot-minimal.nix")
|
|
|
|
];
|
|
|
|
})
|
|
|
|
];
|
|
|
|
}).config.system.build.kexecTree;
|
|
|
|
};
|
|
|
|
|
2022-08-26 16:14:24 +02:00
|
|
|
devShells.default = pkgs.mkShell {
|
2021-05-01 16:30:48 +02:00
|
|
|
buildInputs = (with pkgs; [
|
2021-06-01 00:02:27 +02:00
|
|
|
black
|
2021-05-01 16:30:48 +02:00
|
|
|
nixpkgs-fmt
|
2024-01-06 01:19:35 +01:00
|
|
|
reuse
|
2021-05-31 23:59:13 +02:00
|
|
|
shellcheck
|
2021-05-01 16:30:48 +02:00
|
|
|
sops
|
|
|
|
ssh-to-pgp
|
|
|
|
]);
|
|
|
|
|
|
|
|
shellHook = ''
|
|
|
|
find ${./keys} -type f -print0 | xargs -0 ${pkgs.gnupg}/bin/gpg --quiet --import
|
|
|
|
'' + self.checks.${system}.pre-commit-check.shellHook;
|
|
|
|
};
|
|
|
|
}) // {
|
2022-06-23 19:03:58 +02:00
|
|
|
overlays.default = import ./pkgs;
|
2021-05-01 16:30:48 +02:00
|
|
|
|
|
|
|
nixosConfigurations = nixpkgs.lib.mapAttrs
|
|
|
|
(hostname: { system
|
|
|
|
, extraModules ? [ ]
|
|
|
|
, targetHost ? hostname
|
|
|
|
, unlockOverV4 ? true
|
2021-09-08 20:04:24 +02:00
|
|
|
, nixpkgs ? inputs.nixpkgs
|
2021-05-01 16:30:48 +02:00
|
|
|
}: nixpkgs.lib.nixosSystem rec {
|
|
|
|
inherit system;
|
|
|
|
|
|
|
|
modules = [
|
|
|
|
(./machines + "/${hostname}/configuration.nix")
|
|
|
|
|
|
|
|
{
|
2021-05-15 10:04:44 +02:00
|
|
|
_module.args.inputs = inputs;
|
2021-05-01 16:30:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# deployment settings
|
|
|
|
({ lib, ... }: {
|
|
|
|
options.deployment = {
|
|
|
|
targetHost = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
readOnly = true;
|
|
|
|
internal = true;
|
|
|
|
};
|
|
|
|
unlockOverV4 = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
readOnly = true;
|
|
|
|
internal = true;
|
|
|
|
description = "Whether to unlock the host over IPv4 (only)";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
config.deployment = {
|
|
|
|
inherit
|
|
|
|
targetHost
|
|
|
|
unlockOverV4;
|
|
|
|
};
|
|
|
|
})
|
2021-05-15 10:04:44 +02:00
|
|
|
] ++ (with inputs; [
|
2023-04-16 11:26:09 +02:00
|
|
|
# Use newest home-manager for hosts that use unstable, otherwise there are conflicts
|
|
|
|
(if nixpkgs == inputs.nixpkgs-unstable then home-manager-unstable else home-manager).nixosModules.home-manager
|
2021-05-15 10:04:44 +02:00
|
|
|
sops-nix.nixosModules.sops
|
|
|
|
|
|
|
|
bang-evaluator.nixosModules.bang-evaluator
|
2021-08-28 13:53:38 +02:00
|
|
|
]) ++ (builtins.attrValues nixpkgs-overlay.nixosModules)
|
|
|
|
++ extraModules;
|
2021-05-01 16:30:48 +02:00
|
|
|
})
|
2021-05-15 10:04:44 +02:00
|
|
|
(import ./machines inputs);
|
2021-05-01 16:30:48 +02:00
|
|
|
};
|
|
|
|
}
|