2023-12-28 17:07:40 +00:00
|
|
|
{ self, lib, pkgsFlakes, hmFlakes, inputs, pkgs', config, ... }:
|
2022-02-13 13:10:21 +00:00
|
|
|
let
|
|
|
|
inherit (builtins) attrValues mapAttrs;
|
2022-06-11 16:33:32 +01:00
|
|
|
inherit (lib)
|
2022-08-01 22:55:59 +01:00
|
|
|
substring flatten optional optionals mkIf mkDefault mkForce mkOption mkOptionType foldAttrs mapAttrsToList;
|
2022-06-11 16:33:32 +01:00
|
|
|
inherit (lib.my)
|
2023-05-27 16:57:28 +01:00
|
|
|
homeStateVersion mkOpt' mkBoolOpt' mkDefault' commonOpts inlineModule' applyAssertions duplicates net;
|
2022-02-06 00:06:26 +00:00
|
|
|
|
2022-02-20 15:59:07 +00:00
|
|
|
cfg = config.nixos;
|
2022-02-15 20:50:27 +00:00
|
|
|
|
2022-08-01 22:55:59 +01:00
|
|
|
allAssignments = (mapAttrs (_: c: c.assignments) cfg.systems) // (foldAttrs (c: all: all // c) { } (mapAttrsToList (_: c: c.extraAssignments) cfg.systems));
|
2022-05-17 23:27:14 +01:00
|
|
|
|
2022-02-20 15:59:07 +00:00
|
|
|
mkSystem =
|
|
|
|
{
|
|
|
|
name,
|
|
|
|
config',
|
|
|
|
defs,
|
2022-02-06 00:06:26 +00:00
|
|
|
}:
|
2022-02-13 13:10:21 +00:00
|
|
|
let
|
2022-05-28 18:38:03 +01:00
|
|
|
# The flake contains `nixosSystem`, so we do need it
|
2022-02-20 15:59:07 +00:00
|
|
|
pkgsFlake = pkgsFlakes.${config'.nixpkgs};
|
2022-02-15 20:50:27 +00:00
|
|
|
|
2022-02-20 15:59:07 +00:00
|
|
|
pkgs = pkgs'.${config'.nixpkgs}.${config'.system};
|
|
|
|
allPkgs = mapAttrs (_: p: p.${config'.system}) pkgs';
|
2022-02-15 20:50:27 +00:00
|
|
|
|
2022-02-20 15:59:07 +00:00
|
|
|
modules' = [ hmFlakes.${config'.home-manager}.nixosModule ] ++ (attrValues cfg.modules);
|
2022-02-15 20:50:27 +00:00
|
|
|
in
|
2023-12-28 17:07:40 +00:00
|
|
|
# Import eval-config ourselves since the flake now force-sets lib
|
|
|
|
import "${pkgsFlake}/nixos/lib/eval-config.nix" {
|
2022-02-15 20:50:27 +00:00
|
|
|
# Gotta override lib here unforunately, eval-config.nix likes to import its own (unextended) lib. We explicitly
|
|
|
|
# don't pass pkgs so that it'll be imported with modularly applied config and overlays.
|
2023-12-28 17:07:40 +00:00
|
|
|
lib = pkgs.lib.extend (lib.my.versionOverlay { inherit self pkgsFlake; });
|
|
|
|
|
|
|
|
# Set to null since we pass modularly
|
|
|
|
system = null;
|
2022-02-20 15:59:07 +00:00
|
|
|
|
|
|
|
# Put the inputs in specialArgs to avoid infinite recursion when modules try to do imports
|
2024-07-21 12:37:32 +01:00
|
|
|
specialArgs = { inherit self inputs pkgsFlakes pkgsFlake allAssignments; inherit (cfg) systems; };
|
2022-02-20 15:59:07 +00:00
|
|
|
|
2022-02-15 20:50:27 +00:00
|
|
|
# `baseModules` informs the manual which modules to document
|
|
|
|
baseModules =
|
2022-02-20 15:59:07 +00:00
|
|
|
(import "${pkgsFlake}/nixos/modules/module-list.nix") ++ (optionals config'.docCustom modules');
|
|
|
|
modules = (optionals (!config'.docCustom) modules') ++ [
|
|
|
|
(sysModArgs: {
|
2022-02-15 20:50:27 +00:00
|
|
|
warnings = flatten [
|
2022-02-20 15:59:07 +00:00
|
|
|
(optional (sysModArgs.config.home-manager.useGlobalPkgs && (config'.nixpkgs != config'.home-manager))
|
2022-02-15 20:50:27 +00:00
|
|
|
''
|
2022-02-20 15:59:07 +00:00
|
|
|
Using global nixpkgs ${config'.nixpkgs} with home-manager ${config'.home-manager} may cause problems.
|
2022-02-15 20:50:27 +00:00
|
|
|
'')
|
|
|
|
];
|
|
|
|
|
|
|
|
_module.args = {
|
2022-10-16 19:07:16 +01:00
|
|
|
inherit (cfg) secretsPath vpns;
|
2022-05-17 23:27:14 +01:00
|
|
|
inherit (config') assignments;
|
2022-02-15 20:50:27 +00:00
|
|
|
pkgs' = allPkgs;
|
|
|
|
};
|
|
|
|
|
2023-12-28 17:07:40 +00:00
|
|
|
system = { inherit name; };
|
2022-06-05 16:40:44 +01:00
|
|
|
networking = {
|
2022-06-06 13:25:05 +01:00
|
|
|
domain = let d = config'.assignments.internal.domain or null; in mkIf (d != null) (mkDefault' d);
|
2022-06-05 16:40:44 +01:00
|
|
|
hostName = mkDefault (config'.assignments.internal.name or name);
|
|
|
|
};
|
2022-02-15 20:50:27 +00:00
|
|
|
nixpkgs = {
|
2022-02-20 15:59:07 +00:00
|
|
|
inherit (config') system;
|
2022-05-07 15:12:29 +01:00
|
|
|
# Make sure any previously set overlays (e.g. lib which will be inherited by home-manager down the
|
|
|
|
# line) are passed on when nixpkgs is imported. We don't inherit config anymore because apparently it
|
|
|
|
# doesn't seem to merge properly... (https://github.com/NixOS/nixpkgs/blob/14a348fcc6c0d28804f640375f058d5491c2e1ee/nixos/modules/misc/nixpkgs.nix#L34)
|
|
|
|
# TODO: Possible this behaviour will be fixed in future?
|
|
|
|
inherit (pkgs) overlays;
|
2022-02-15 20:50:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# Unfortunately it seems there's no way to fully decouple home-manager's lib from NixOS's pkgs.lib. :(
|
|
|
|
# https://github.com/nix-community/home-manager/blob/7c2ae0bdd20ddcaafe41ef669226a1df67f8aa06/nixos/default.nix#L22
|
|
|
|
home-manager = {
|
2022-02-20 15:59:07 +00:00
|
|
|
extraSpecialArgs = { inherit inputs; };
|
2022-02-15 20:50:27 +00:00
|
|
|
# Optimise if system and home-manager nixpkgs are the same
|
2022-05-28 18:38:03 +01:00
|
|
|
useGlobalPkgs = mkDefault (config'.nixpkgs == config'.hmNixpkgs);
|
2022-02-20 15:59:07 +00:00
|
|
|
sharedModules = (attrValues config.home-manager.modules) ++ [
|
2022-02-15 20:50:27 +00:00
|
|
|
{
|
|
|
|
warnings = flatten [
|
2022-02-20 15:59:07 +00:00
|
|
|
(optional (!sysModArgs.config.home-manager.useGlobalPkgs && (config'.hmNixpkgs != config'.home-manager))
|
2022-02-15 20:50:27 +00:00
|
|
|
''
|
2022-02-20 15:59:07 +00:00
|
|
|
Using per-user nixpkgs ${config'.hmNixpkgs} with home-manager ${config'.home-manager}
|
|
|
|
may cause issues.
|
2022-02-15 20:50:27 +00:00
|
|
|
'')
|
|
|
|
];
|
|
|
|
|
2022-02-20 15:59:07 +00:00
|
|
|
# pkgsPath is used by home-manager's nixpkgs module to import nixpkgs (i.e. if !useGlobalPkgs)
|
2022-02-15 20:50:27 +00:00
|
|
|
_module.args = {
|
2022-02-20 15:59:07 +00:00
|
|
|
pkgsPath = toString pkgsFlakes.${config'.hmNixpkgs};
|
2022-02-15 20:50:27 +00:00
|
|
|
pkgs' = allPkgs;
|
|
|
|
};
|
2023-12-28 17:07:40 +00:00
|
|
|
|
|
|
|
home.enableNixpkgsReleaseCheck = false;
|
2022-02-15 20:50:27 +00:00
|
|
|
}
|
2022-02-20 15:59:07 +00:00
|
|
|
(homeStateVersion config'.home-manager)
|
2022-02-15 20:50:27 +00:00
|
|
|
];
|
|
|
|
};
|
|
|
|
})
|
2022-02-20 15:59:07 +00:00
|
|
|
] ++ defs;
|
|
|
|
};
|
|
|
|
|
2022-05-17 23:27:14 +01:00
|
|
|
assignmentOpts = with lib.types; { name, config, ... }: {
|
|
|
|
options = {
|
|
|
|
name = mkOpt' str name "Name of assignment.";
|
|
|
|
altNames = mkOpt' (listOf str) [ ] "Extra names to assign.";
|
|
|
|
visible = mkBoolOpt' true "Whether or not this assignment should be visible.";
|
2022-06-05 16:40:44 +01:00
|
|
|
domain = mkOpt' (nullOr str) null "Domain for this assignment.";
|
2023-12-16 15:59:33 +00:00
|
|
|
mtu = mkOpt' (nullOr ints.unsigned) null "Interface MTU.";
|
2022-05-17 23:27:14 +01:00
|
|
|
ipv4 = {
|
2023-05-27 16:57:28 +01:00
|
|
|
address = mkOpt' net.types.ipv4 null "IPv4 address.";
|
2022-05-17 23:27:14 +01:00
|
|
|
mask = mkOpt' ints.u8 24 "Network mask.";
|
2023-05-27 16:57:28 +01:00
|
|
|
gateway =
|
|
|
|
mkOpt' (nullOr str) (net.cidr.host 1 "${config.ipv4.address}/${toString config.ipv4.mask}") "IPv4 gateway.";
|
2022-06-05 16:21:24 +01:00
|
|
|
genPTR = mkBoolOpt' true "Whether to generate a PTR record.";
|
2022-05-17 23:27:14 +01:00
|
|
|
};
|
|
|
|
ipv6 = {
|
2023-05-27 16:57:28 +01:00
|
|
|
address = mkOpt' (nullOr net.types.ipv6) null "IPv6 address.";
|
2022-05-17 23:27:14 +01:00
|
|
|
mask = mkOpt' ints.u8 64 "Network mask.";
|
2022-05-29 03:30:40 +01:00
|
|
|
iid = mkOpt' (nullOr str) null "SLAAC static address.";
|
2022-05-17 23:27:14 +01:00
|
|
|
gateway = mkOpt' (nullOr str) null "IPv6 gateway.";
|
2022-06-05 16:21:24 +01:00
|
|
|
genPTR = mkBoolOpt' true "Whether to generate a PTR record.";
|
2022-05-17 23:27:14 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-10-16 19:07:16 +01:00
|
|
|
l2PeerOpts = with lib.types; {
|
|
|
|
options = {
|
|
|
|
addr = mkOpt' str null "Address.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
l2MeshOpts = with lib.types; { name, ... }: {
|
|
|
|
options = {
|
|
|
|
interface = mkOpt' str name "Name of VXLAN interface.";
|
2023-11-26 01:29:44 +00:00
|
|
|
ipv6 = mkBoolOpt' false "Whether this mesh's underlay operates over IPv6.";
|
|
|
|
baseMTU = mkOpt' ints.unsigned 1500 "Base MTU to calculate VXLAN MTU with.";
|
|
|
|
l3Overhead = mkOpt' ints.unsigned 40 "Overhead of L3 header (to calculate MTU).";
|
2024-03-23 12:04:07 +00:00
|
|
|
udpEncapsulation = mkBoolOpt' false "Whether to encapsulate ESP frames in UDP.";
|
2022-10-16 19:07:16 +01:00
|
|
|
firewall = mkBoolOpt' true "Whether to generate firewall rules.";
|
|
|
|
vni = mkOpt' ints.unsigned 1 "VXLAN VNI.";
|
|
|
|
peers = mkOpt' (attrsOf (submodule l2PeerOpts)) { } "Peers.";
|
|
|
|
security = {
|
|
|
|
enable = mkBoolOpt' true "Whether to enable IPsec authentication.";
|
|
|
|
encrypt = mkBoolOpt' false "Whether to enable IPsec encryption.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-06-11 16:33:32 +01:00
|
|
|
systemOpts = with lib.types; { name, ... }@args:
|
|
|
|
let
|
|
|
|
config' = args.config;
|
|
|
|
in
|
|
|
|
{
|
2022-02-20 15:59:07 +00:00
|
|
|
options = {
|
|
|
|
inherit (commonOpts) system nixpkgs home-manager;
|
|
|
|
hmNixpkgs = commonOpts.nixpkgs;
|
|
|
|
# This causes a (very slow) docs rebuild on every change to a module's options it seems
|
2022-02-21 01:15:27 +00:00
|
|
|
# TODO: Currently broken with infinite recursion...
|
2022-02-20 15:59:07 +00:00
|
|
|
docCustom = mkBoolOpt' false "Whether to document nixfiles' custom NixOS modules.";
|
|
|
|
|
2022-05-17 23:27:14 +01:00
|
|
|
assignments = mkOpt' (attrsOf (submoduleWith {
|
2022-05-28 15:30:12 +01:00
|
|
|
modules = [ assignmentOpts { _module.args.name = mkForce name; } ];
|
2022-05-17 23:27:14 +01:00
|
|
|
})) { } "Network assignments.";
|
2022-08-01 22:55:59 +01:00
|
|
|
# TODO: Getting the default name for the extra assignment is currently fucked for the same reason as above
|
|
|
|
extraAssignments = mkOpt' (attrsOf (attrsOf (submodule assignmentOpts))) { } "Extra network assignments.";
|
2022-05-17 23:27:14 +01:00
|
|
|
|
2022-02-20 15:59:07 +00:00
|
|
|
configuration = mkOption {
|
|
|
|
description = "NixOS configuration module.";
|
|
|
|
# Based on the definition of containers.<name>.config
|
|
|
|
type = mkOptionType {
|
|
|
|
name = "Toplevel NixOS config";
|
2022-06-11 16:33:32 +01:00
|
|
|
merge = _: defs: applyAssertions config (mkSystem {
|
|
|
|
inherit name config';
|
2022-02-20 15:59:07 +00:00
|
|
|
defs = map (d: inlineModule' d.file d.value) defs;
|
2022-06-11 16:33:32 +01:00
|
|
|
});
|
2022-02-20 15:59:07 +00:00
|
|
|
};
|
|
|
|
};
|
2023-11-16 21:42:30 +00:00
|
|
|
rendered = mkOpt' unspecified config'.configuration "Final NixOS modules system output.";
|
2022-02-06 00:06:26 +00:00
|
|
|
};
|
2022-05-28 18:38:03 +01:00
|
|
|
|
|
|
|
config = {
|
2022-06-11 16:33:32 +01:00
|
|
|
home-manager = mkDefault config'.nixpkgs;
|
|
|
|
hmNixpkgs = mkDefault config'.nixpkgs;
|
2022-05-28 18:38:03 +01:00
|
|
|
};
|
2022-02-13 13:10:21 +00:00
|
|
|
};
|
2022-02-20 15:59:07 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
imports = [ modules/_list.nix ];
|
2022-02-17 15:47:24 +00:00
|
|
|
|
2022-02-20 15:59:07 +00:00
|
|
|
options = with lib.types; {
|
|
|
|
nixos = {
|
2022-02-22 00:59:57 +00:00
|
|
|
secretsPath = mkOpt' path null "Path to encrypted secret files.";
|
2022-02-20 15:59:07 +00:00
|
|
|
modules = mkOpt' (attrsOf commonOpts.moduleType) { } "NixOS modules to be exported by nixfiles.";
|
|
|
|
systems = mkOpt' (attrsOf (submodule systemOpts)) { } "NixOS systems to be exported by nixfiles.";
|
2023-12-09 15:22:01 +00:00
|
|
|
allAssignments = mkOption {
|
|
|
|
type = attrsOf (attrsOf (submodule assignmentOpts));
|
|
|
|
description = "All network assignments.";
|
|
|
|
readOnly = true;
|
|
|
|
};
|
2022-10-16 19:07:16 +01:00
|
|
|
vpns = {
|
|
|
|
l2 = mkOpt' (attrsOf (submodule l2MeshOpts)) { } "Layer 2 meshes.";
|
|
|
|
};
|
2022-02-20 15:59:07 +00:00
|
|
|
};
|
2022-02-17 15:47:24 +00:00
|
|
|
};
|
2022-06-11 16:33:32 +01:00
|
|
|
|
|
|
|
config = {
|
|
|
|
assertions =
|
|
|
|
let
|
|
|
|
assignedIPs =
|
|
|
|
flatten
|
|
|
|
(map
|
|
|
|
(as:
|
|
|
|
map
|
2022-11-21 01:21:50 +00:00
|
|
|
(a: [ a.ipv4.address ] ++ (optional (a.ipv6.address != null) a.ipv6.address) )
|
2022-06-11 16:33:32 +01:00
|
|
|
(attrValues as))
|
|
|
|
(attrValues allAssignments));
|
|
|
|
dupIPs = duplicates assignedIPs;
|
|
|
|
in
|
|
|
|
[
|
|
|
|
{
|
|
|
|
assertion = dupIPs == [ ];
|
|
|
|
message = "Duplicate assignments: ${toString dupIPs}";
|
|
|
|
}
|
|
|
|
];
|
2023-12-09 15:22:01 +00:00
|
|
|
|
|
|
|
nixos = {
|
|
|
|
inherit allAssignments;
|
|
|
|
};
|
2022-06-11 16:33:32 +01:00
|
|
|
};
|
2022-02-13 13:10:21 +00:00
|
|
|
}
|