Files

333 lines
9.5 KiB
Nix

{
description = "Zentralwerk network";
inputs = {
bird = {
url = "github:NuschtOS/bird.nix";
inputs = {
flake-utils.follows = "nixos-modules/flake-utils";
nixpkgs.follows = "nixpkgs";
};
};
c3d2-user-module = {
url = "git+https://gitea.c3d2.de/c3d2/nix-user-module.git";
inputs = {
nixos-modules.follows = "nixos-modules";
nixpkgs.follows = "nixpkgs";
};
};
disko = {
url = "github:nix-community/disko";
inputs.nixpkgs.follows = "nixpkgs";
};
dns-nix = {
url = "github:nix-community/dns.nix";
inputs = {
flake-utils.follows = "nixos-modules/flake-utils";
nixpkgs.follows = "nixpkgs";
};
};
haumea = {
url = "github:nix-community/haumea";
inputs.nixpkgs.follows = "nixpkgs";
};
namaka = {
url = "github:nix-community/namaka";
inputs = {
haumea.follows = "haumea";
nixpkgs.follows = "nixpkgs";
};
};
nix-defaults = {
url = "git+https://gitea.c3d2.de/c3d2/nix-defaults.git";
};
nixos-modules = {
url = "github:NuschtOS/nixos-modules";
inputs.nixpkgs.follows = "nixpkgs";
};
nixpkgs.url = "github:NuschtOS/nuschtpkgs/backports-26.05";
nixpkgs-unstable.url = "github:NuschtOS/nuschtpkgs/nixos-unstable";
openwrt = {
url = "git+https://git.openwrt.org/openwrt/openwrt.git?ref=openwrt-25.12";
flake = false;
};
openwrt-imagebuilder = {
url = "github:astro/nix-openwrt-imagebuilder";
inputs = {
nixpkgs.follows = "nixpkgs";
systems.follows = "nixos-modules/flake-utils/systems";
};
};
openwrt-packages = {
url = "git+https://gitea.c3d2.de/c3d2/openwrt-packages.git";
flake = false;
};
pogo = {
url = "github:nuschtos/pogo";
inputs = {
disko.follows = "disko";
flake-utils.follows = "nixos-modules/flake-utils";
haumea.follows = "haumea";
namaka.follows = "namaka";
nixpkgs.follows = "nixpkgs";
};
};
sops-nix = {
url = "github:Mic92/sops-nix";
inputs = {
nixpkgs.follows = "nixpkgs";
};
};
switch = {
url = "git+https://gitea.c3d2.de/zentralwerk/switch.git";
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.follows = "nixos-modules/flake-utils";
};
};
outputs = inputs@{
self,
bird,
c3d2-user-module,
dns-nix,
namaka,
nix-defaults,
nixos-modules,
nixpkgs,
openwrt,
openwrt-imagebuilder,
openwrt-packages,
pogo,
sops-nix,
...
}:
let
inherit (nixpkgs) lib;
system = "x86_64-linux";
systems = [ system ];
forAllSystems = lib.genAttrs systems;
secrets' = ./config/secrets-production.nix;
network-lib' = { secrets, systemArg ? system }: import ./nix/lib {
inherit openwrt secrets;
inherit (nixpkgs.legacyPackages.${systemArg}) lib pkgs;
};
nixosConfig = { hostName, modules ? [ ], secrets ? secrets' }@args:
{ inherit args; } //
(let
network-lib = network-lib' { inherit secrets; };
in lib.nixosSystem {
inherit system;
modules = [
./nix/nixos-module
bird.nixosModules.bird
c3d2-user-module.nixosModule
nix-defaults.nixosModules.defaults
nixos-modules.nixosModule
{ inherit (network-lib.config) site; }
] ++ modules;
specialArgs = {
inherit hostName network-lib;
inherit (inputs) nixpkgs switch;
inherit self;
dns-util = dns-nix.lib;
};
});
in {
inherit network-lib';
network-lib = network-lib' { secrets = secrets'; };
# Everything that can be built locally outside of NixOS
packages' = { secrets }: forAllSystems (system: let
network-lib = network-lib' { inherit secrets; };
in
import ./nix/pkgs {
inherit self;
inherit (network-lib) config;
inherit (self) lastModifiedDate;
inherit network-lib nixpkgs openwrt-imagebuilder openwrt-packages system;
inherit (inputs) switch;
nixosConfigurations = self.nixosConfigurations' { inherit secrets; };
}
);
packages = self.packages' { secrets = secrets'; };
# NixOS host systems (servers, and containers)
nixosConfigurations' = { secrets }:
(builtins.mapAttrs (hostName: _: nixosConfig { inherit hostName secrets; }) (let
network-lib = network-lib' { inherit secrets; };
in
lib.filterAttrs (_: { role, ... }:
builtins.elem role [ "container" ]
) network-lib.config.site.hosts
)) // {
network1 = nixosConfig {
inherit secrets;
hostName = "network1";
modules = [
./nix/nixos-module/server
./nix/hosts/network1
pogo.nixosModules.default
sops-nix.nixosModules.default
];
};
network2 = nixosConfig {
inherit secrets;
hostName = "network2";
modules = [
./nix/nixos-module/server
./nix/hosts/network2
pogo.nixosModules.default
sops-nix.nixosModules.default
];
};
dnscache = nixosConfig {
inherit secrets;
hostName = "dnscache";
modules = [
./nix/container/dnscache.nix
];
};
mgmt-gw = nixosConfig {
inherit secrets;
hostName = "mgmt-gw";
modules = [
./nix/container/mgmt-gw.nix
];
};
mgmt-ap-gw = nixosConfig {
inherit secrets;
hostName = "mgmt-ap-gw";
modules = [
./nix/container/mgmt-ap-gw.nix
];
};
time = nixosConfig {
inherit secrets;
hostName = "time";
modules = [
./nix/container/time.nix
];
};
vpn-adm-gw = nixosConfig {
inherit secrets;
hostName = "vpn-adm-gw";
modules = [
./nix/container/vpn.nix
];
};
vpn-gw = nixosConfig {
inherit secrets;
hostName = "vpn-gw";
modules = [
./nix/container/vpn.nix
];
};
rs01 = nixosConfig {
inherit secrets;
hostName = "rs01";
modules = [
./nix/container/routeserver.nix
];
};
rs02 = nixosConfig {
inherit secrets;
hostName = "rs02";
modules = [
./nix/container/routeserver.nix
];
};
};
nixosConfigurations = self.nixosConfigurations' { secrets = secrets'; };
# For `nix flake check`, and Hydra
checks = {
namaka = namaka.lib.load {
# regen with ./scripts/generate-tests.sh
src = ./nix/tests;
inputs = {
eval = containerName: let
inherit (self.nixosConfigurations."${containerName}") config;
in lib.genAttrs (
lib.optional config.services.bird.enable "bird/bird.conf"
++ lib.optional config.services.kea.ctrl-agent.enable "kea/ctrl-agent.conf"
++ lib.optional config.services.kea.dhcp-ddns.enable "kea/dhcp-ddns.conf"
++ lib.optional config.services.kea.dhcp4.enable "kea/dhcp4-server.conf"
++ lib.optional config.services.kea.dhcp6.enable "kea/dhcp6-server.conf"
) (etcFile:
let
inherit (config.environment.etc."${etcFile}") source;
in
(if lib.hasPrefix "kea" etcFile then
x: lib.filterAttrsRecursive (n: _v: n != "hooks-libraries") (builtins.fromJSON x)
else
lib.id)
(source.value or source.text)
);
};
};
inherit (self) packages;
};
devShell = lib.mapAttrs (system: sopsPkgs:
with nixpkgs.legacyPackages.${system};
mkShell {
sopsPGPKeyDirs = [ "./keys" ];
nativeBuildInputs = [
bashInteractive
namaka.packages.${system}.default
sopsPkgs.sops-import-keys-hook
];
}
) sops-nix.packages;
hydraJobs =
let
exportFileWrapper = pkg:
lib.hydraJob (
nixpkgs.legacyPackages.${system}.runCommand "${pkg.name}-exported" {
src = pkg;
} ''
mkdir -p $out/nix-support
ln -s ${pkg} $out/${pkg.name}
echo file source-dist $out/${pkg.name} >> $out/nix-support/hydra-build-products
''
);
wrappers = {
"export-config" = exportFileWrapper;
"switch-.*" = exportFileWrapper;
"ap.*-image" = exportFileWrapper;
};
in
builtins.mapAttrs (name: pkg:
builtins.foldl' (result: wrapperName:
if builtins.match wrapperName name != null
then wrappers.${wrapperName} pkg
else lib.hydraJob result
) pkg (builtins.attrNames wrappers)
) (lib.filterAttrs (n: _v: !lib.hasSuffix "-uciConfig" n) (self.packages' { secrets = ./config/secrets.nix; }).${system}) // {
namake = lib.hydraJob self.checks.namaka;
};
};
}