Compare commits

..

No commits in common. "5341235bc3435efa4cf3015954e06ca278570c1f" and "2352a1a9177fbb7d3fed042cc74f80e079196020" have entirely different histories.

22 changed files with 1066 additions and 1208 deletions

View file

@ -1,21 +1,21 @@
{
self,
deploy-rs,
...
}: let
{ self, deploy-rs, ... }:
let
deployArch = "x86_64-linux";
mkDeployNodes = nodeDef:
builtins.mapAttrs
(name: machine: nodeDef name machine)
self.machines.${deployArch};
in {
in
{
deploy = {
sshUser = "root";
user = "root";
nodes = mkDeployNodes (name: machine: let
nodes = mkDeployNodes (name: machine:
let
nixosConfiguration = self.nixosConfigurations.${name};
in {
in
{
hostname = nixosConfiguration.config.networking.fqdn;
profiles.system = {
remoteBuild = machine.arch != deployArch;

116
flake.lock generated
View file

@ -109,22 +109,6 @@
"type": "github"
}
},
"flake-compat_4": {
"flake": false,
"locked": {
"lastModified": 1696426674,
"narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "0f9255e01c2351cc7d116c072cb317785dd33b33",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"flake-parts": {
"inputs": {
"nixpkgs-lib": [
@ -179,27 +163,6 @@
"type": "github"
}
},
"gitignore": {
"inputs": {
"nixpkgs": [
"pre-commit-hooks",
"nixpkgs"
]
},
"locked": {
"lastModified": 1709087332,
"narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=",
"owner": "hercules-ci",
"repo": "gitignore.nix",
"rev": "637db329424fd7e46cf4185293b9cc8c88c95394",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "gitignore.nix",
"type": "github"
}
},
"kubenix": {
"inputs": {
"flake-compat": "flake-compat_2",
@ -299,22 +262,6 @@
}
},
"nixpkgs-stable": {
"locked": {
"lastModified": 1720386169,
"narHash": "sha256-NGKVY4PjzwAa4upkGtAMz1npHGoRzWotlSnVlqI40mo=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "194846768975b7ad2c4988bdb82572c00222c0d7",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-24.05",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-stable_2": {
"locked": {
"lastModified": 1729357638,
"narHash": "sha256-66RHecx+zohbZwJVEPF7uuwHeqf8rykZTMCTqIrOew4=",
@ -362,45 +309,6 @@
"type": "github"
}
},
"nixpkgs_3": {
"locked": {
"lastModified": 1726871744,
"narHash": "sha256-V5LpfdHyQkUF7RfOaDPrZDP+oqz88lTJrMT1+stXNwo=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "a1d92660c6b3b7c26fb883500a80ea9d33321be2",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"pre-commit-hooks": {
"inputs": {
"flake-compat": "flake-compat_4",
"gitignore": "gitignore",
"nixpkgs": [
"nixpkgs-unstable"
],
"nixpkgs-stable": "nixpkgs-stable"
},
"locked": {
"lastModified": 1729104314,
"narHash": "sha256-pZRZsq5oCdJt3upZIU4aslS9XwFJ+/nVtALHIciX/BI=",
"owner": "cachix",
"repo": "git-hooks.nix",
"rev": "3c3e88f0f544d6bb54329832616af7eb971b6be6",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "git-hooks.nix",
"type": "github"
}
},
"root": {
"inputs": {
"deploy-rs": "deploy-rs",
@ -413,9 +321,7 @@
"nixos-hardware": "nixos-hardware",
"nixpkgs": "nixpkgs_2",
"nixpkgs-unstable": "nixpkgs-unstable",
"pre-commit-hooks": "pre-commit-hooks",
"sops-nix": "sops-nix",
"treefmt-nix": "treefmt-nix"
"sops-nix": "sops-nix"
}
},
"sops-nix": {
@ -423,7 +329,7 @@
"nixpkgs": [
"nixpkgs"
],
"nixpkgs-stable": "nixpkgs-stable_2"
"nixpkgs-stable": "nixpkgs-stable"
},
"locked": {
"lastModified": 1729775275,
@ -504,24 +410,6 @@
"type": "github"
}
},
"treefmt-nix": {
"inputs": {
"nixpkgs": "nixpkgs_3"
},
"locked": {
"lastModified": 1730025913,
"narHash": "sha256-Y9NtFmP8ciLyRsopcCx1tyoaaStKeq+EndwtGCgww7I=",
"owner": "numtide",
"repo": "treefmt-nix",
"rev": "bae131e525cc8718da22fbeb8d8c7c43c4ea502a",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "treefmt-nix",
"type": "github"
}
},
"utils": {
"inputs": {
"systems": "systems"

View file

@ -17,12 +17,6 @@
deploy-rs.url = "github:serokell/deploy-rs";
nixos-hardware.url = "github:NixOS/nixos-hardware/master";
flake-utils.url = "github:numtide/flake-utils";
treefmt-nix.url = "github:numtide/treefmt-nix";
pre-commit-hooks = {
url = "github:cachix/git-hooks.nix";
inputs.nixpkgs.follows = "nixpkgs-unstable";
};
disko = {
url = "github:nix-community/disko";
@ -55,11 +49,8 @@
};
};
outputs = inputs @ {
nixpkgs,
flake-utils,
...
}:
outputs =
inputs@{ nixpkgs, flake-utils, ... }:
flake-utils.lib.meld inputs [
./scripts
./deploy.nix
@ -67,6 +58,7 @@
./shell.nix
./utils
./machines
./formatter.nix
];
] // (flake-utils.lib.eachDefaultSystem (system: {
formatter = nixpkgs.legacyPackages.${system}.nixfmt;
}));
}

View file

@ -1,14 +0,0 @@
{
nixpkgs,
treefmt-nix,
flake-utils,
...
}:
flake-utils.lib.eachDefaultSystem (
system: let
pkgs = nixpkgs.legacyPackages.${system};
treefmtEval = treefmt-nix.lib.evalModule pkgs ./treefmt.nix;
in {
formatter = treefmtEval.config.build.wrapper;
}
)

View file

@ -1,9 +1,5 @@
{
nixpkgs,
flake-utils,
...
}:
flake-utils.lib.eachDefaultSystem (system: let
{ nixpkgs, flake-utils, ... }: flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
lib = pkgs.lib;
@ -47,9 +43,9 @@ flake-utils.lib.eachDefaultSystem (system: let
};
};
};
in {
machines =
(lib.modules.evalModules {
in
{
machines = (lib.modules.evalModules {
modules = [
allOpts
./warwick.nix
@ -59,7 +55,5 @@ in {
# ./talos.nix
# ./pikvm.nix
];
})
.config
.machines;
}).config.machines;
})

View file

@ -3,12 +3,7 @@
arch = "aarch64-linux";
isRaspberryPi = true;
nixosModule = {
config,
inputs,
lib,
...
}: {
nixosModule = { config, inputs, lib, ... }: {
# imports = [ "${inputs.nixpkgs}/nixos/modules/installer/sd-card/sd-image-aarch64.nix" ];
lab = {
storage.profile = "pi";

View file

@ -1,9 +1,5 @@
{
pkgs,
lib,
config,
...
}: let
{ pkgs, lib, config, ... }:
let
cfg = config.lab.backups;
borgmaticConfig = pkgs.writeTextFile {
@ -31,7 +27,8 @@
encryption_passcommand = "${pkgs.coreutils}/bin/cat ${config.sops.secrets."borg/borgPassphrase".path}";
};
};
in {
in
{
options.lab.backups = {
enable = lib.mkOption {
default = false;

View file

@ -1,8 +1,5 @@
{
lib,
config,
...
}: let
{ lib, config, ... }:
let
cfg = config.lab.data-sharing;
nfsShares = [
@ -16,12 +13,13 @@
nfsExports = lib.strings.concatLines (
builtins.map
(
share: "${share} 192.168.30.0/16(rw,sync,no_subtree_check,no_root_squash) 127.0.0.1/8(rw,sync,no_subtree_check,no_root_squash) 10.0.0.0/8(rw,sync,no_subtree_check,no_root_squash)"
(share:
"${share} 192.168.30.0/16(rw,sync,no_subtree_check,no_root_squash) 127.0.0.1/8(rw,sync,no_subtree_check,no_root_squash) 10.0.0.0/8(rw,sync,no_subtree_check,no_root_squash)"
)
nfsShares
);
in {
in
{
options.lab.data-sharing = {
enable = lib.mkOption {
default = false;

View file

@ -1,14 +1,5 @@
{
self,
pkgs,
config,
lib,
inputs,
machine,
...
}: {
imports =
[
{ self, pkgs, config, lib, inputs, machine, ... }: {
imports = [
./storage.nix
./backups.nix
./networking
@ -20,8 +11,7 @@
inputs.disko.nixosModules.disko
inputs.sops-nix.nixosModules.sops
inputs.nix-snapshotter.nixosModules.nix-snapshotter
]
++ lib.lists.optional (machine.isRaspberryPi) inputs.nixos-hardware.nixosModules.raspberry-pi-4;
] ++ lib.lists.optional (machine.isRaspberryPi) inputs.nixos-hardware.nixosModules.raspberry-pi-4;
config = {
time.timeZone = "Europe/Amsterdam";
@ -41,9 +31,7 @@
i18n = {
defaultLocale = "en_US.UTF-8";
extraLocaleSettings = let
extraLocale = "nl_NL.UTF-8";
in {
extraLocaleSettings = let extraLocale = "nl_NL.UTF-8"; in {
LC_ADDRESS = extraLocale;
LC_IDENTIFICATION = extraLocale;
LC_MEASUREMENT = extraLocale;

View file

@ -1,13 +1,8 @@
{
self,
inputs,
pkgs,
lib,
config,
...
}: let
{ self, inputs, pkgs, lib, config, ... }:
let
cfg = config.lab.k3s;
in {
in
{
options.lab.k3s = {
enable = lib.mkOption {
default = false;
@ -67,7 +62,8 @@ in {
address = "/run/nix-snapshotter/nix-snapshotter.sock";
};
plugins = let
plugins =
let
k3s-cni-plugins = pkgs.buildEnv {
name = "k3s-cni-plugins";
paths = with pkgs; [
@ -75,7 +71,8 @@ in {
cni-plugin-flannel
];
};
in {
in
{
"io.containerd.grpc.v1.cri" = {
stream_server_address = "127.0.0.1";
stream_server_port = "10010";
@ -94,12 +91,10 @@ in {
};
};
"io.containerd.transfer.v1.local".unpack_config = [
{
"io.containerd.transfer.v1.local".unpack_config = [{
platform = "linux/amd64";
snapshotter = "nix";
}
];
}];
};
};
};
@ -107,7 +102,8 @@ in {
services = {
nix-snapshotter.enable = true;
k3s = let
k3s =
let
serverFlagList = [
"--image-service-endpoint=unix:///run/nix-snapshotter/nix-snapshotter.sock"
"--snapshotter=overlayfs"
@ -119,7 +115,8 @@ in {
];
serverFlags = builtins.concatStringsSep " " serverFlagList;
in {
in
{
enable = true;
role = cfg.role;
tokenFile = config.sops.secrets."k3s/serverToken".path;
@ -146,14 +143,11 @@ in {
k3s-bootstrap = lib.mkIf (cfg.role == "server") {
text = (
let
k3sBootstrapFile =
(inputs.kubenix.evalModules.x86_64-linux {
k3sBootstrapFile = (inputs.kubenix.evalModules.x86_64-linux {
module = import ./bootstrap.nix;
})
.config
.kubernetes
.result;
in ''
}).config.kubernetes.result;
in
''
mkdir -p /var/lib/rancher/k3s/server/manifests
ln -sf ${k3sBootstrapFile} /var/lib/rancher/k3s/server/manifests/k3s-bootstrap.json
''
@ -172,9 +166,11 @@ in {
};
};
sops.secrets = let
sops.secrets =
let
keyPathBase = "/var/lib/rancher/k3s/server/tls";
in {
in
{
"k3s/serverToken" = { };
"k3s/keys/clientCAKey".path = "${keyPathBase}/client-ca.key";
"k3s/keys/requestHeaderCAKey".path = "${keyPathBase}/request-header-ca.key";

View file

@ -1,11 +1,8 @@
{
lib,
config,
machines,
...
}: let
{ lib, config, machines, ... }:
let
cfg = config.lab.monitoring;
in {
in
{
options = {
lab.monitoring = {
enable = lib.mkOption {
@ -21,8 +18,7 @@ in {
};
config = lib.mkIf cfg.enable {
networking.firewall.allowedTCPPorts =
[config.services.prometheus.exporters.node.port]
networking.firewall.allowedTCPPorts = [ config.services.prometheus.exporters.node.port ]
++ lib.lists.optionals cfg.server.enable [ 80 ];
services.prometheus = {
@ -36,15 +32,12 @@ in {
scrapeConfigs = lib.mkIf cfg.server.enable (
let
generated =
lib.attrsets.mapAttrsToList
generated = lib.attrsets.mapAttrsToList
(name: machine: {
job_name = name;
static_configs = [
{
static_configs = [{
targets = [ "${name}.dmz:${toString config.services.prometheus.exporters.node.port}" ];
}
];
}];
})
machines;
@ -60,11 +53,9 @@ in {
password = "admin";
};
static_configs = [
{
static_configs = [{
targets = [ "pikvm.dmz" ];
}
];
}];
};
in
generated ++ [ pikvm ]

View file

@ -1,8 +1,4 @@
{
lib,
machine,
...
}: {
{ lib, machine, ... }: {
config = {
networking = {
domain = "dmz";

View file

@ -1,8 +1,5 @@
{
lib,
config,
...
}: let
{ lib, config, ... }:
let
cfg = config.lab.storage;
modules = [
{
@ -158,7 +155,8 @@
};
}
];
in {
in
{
imports = modules;
options.lab.storage = {

View file

@ -1,10 +1,8 @@
{
lib,
config,
...
}: let
{ lib, config, ... }:
let
cfg = config.lab.tailscale;
in {
in
{
options = {
lab.tailscale = {
enable = lib.mkEnableOption "tailscale";
@ -23,12 +21,10 @@ in {
useRoutingFeatures = "server";
openFirewall = true;
extraUpFlags =
[
extraUpFlags = [
"--accept-dns=false"
"--hostname=${config.networking.hostName}"
]
++ lib.lists.optional cfg.advertiseExitNode "--advertise-exit-node"
] ++ lib.lists.optional cfg.advertiseExitNode "--advertise-exit-node"
++ lib.lists.optional cfg.advertiseExitNode "--advertise-routes=192.168.30.0/24";
};

View file

@ -1,18 +1,15 @@
{
self,
nixpkgs,
...
} @ inputs: let
{ self, nixpkgs, ... }@inputs:
let
deployArch = "x86_64-linux";
machines = self.machines.${deployArch};
mkNixosSystems = systemDef:
builtins.mapAttrs
(
name: machine:
(name: machine:
nixpkgs.lib.nixosSystem (systemDef name machine)
)
machines;
in {
in
{
nixosConfigurations = mkNixosSystems (name: machine: {
system = machine.arch;

View file

@ -1,17 +1,8 @@
{
nixpkgs,
flake-utils,
...
}:
flake-utils.lib.eachDefaultSystem (system: let
{ nixpkgs, flake-utils, ... }: flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
createScript = {
name,
runtimeInputs,
scriptPath,
extraWrapperFlags ? "",
...
}: let
createScript = { name, runtimeInputs, scriptPath, extraWrapperFlags ? "", ... }:
let
script = (pkgs.writeScriptBin name (builtins.readFile scriptPath)).overrideAttrs (old: {
buildCommand = "${old.buildCommand}\n patchShebangs $out";
});
@ -22,7 +13,8 @@ flake-utils.lib.eachDefaultSystem (system: let
buildInputs = [ pkgs.makeWrapper ];
postBuild = "wrapProgram $out/bin/${name} --set PATH $out/bin ${extraWrapperFlags}";
};
in {
in
{
packages.bootstrap = createScript {
name = "bootstrap";
runtimeInputs = with pkgs; [ sops coreutils nixos-anywhere ];

View file

@ -1,11 +1,8 @@
{
flake-utils,
nixpkgs,
...
}:
flake-utils.lib.eachDefaultSystem (system: let
{ flake-utils, nixpkgs, ... }: flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
in {
in
{
devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [ ansible ];
};

View file

@ -1,4 +0,0 @@
{...}: {
projectRootFile = "flake.nix";
programs.alejandra.enable = true;
}

View file

@ -1,12 +1,12 @@
{
nixpkgs,
flake-utils,
...
}: let
systemAttrs = flake-utils.lib.eachDefaultSystem (system: let
{ nixpkgs, flake-utils, ... }:
let
systemAttrs = flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacypackages.${system};
lib = pkgs.lib;
in {
in
{
net = import ./net.nix lib;
});

View file

@ -1,9 +1,13 @@
# IP address arithmetic and validation in Nix by @duairc:
# https://gist.github.com/duairc/5c9bb3c922e5d501a1edb9e7b3b845ba
lib: let
net =
{
lib:
let
net = {
ip = {
# add :: (ip | mac | integer) -> ip -> ip
#
# Examples:
@ -35,7 +39,8 @@ lib: let
# Adding MAC to IPv6:
# > net.ip.add "fe:ed:fa:ce:f0:0d" "dead:cafe:beef::"
# "dead:cafe:beef::feed:face:f00d"
add = delta: ip: let
add = delta: ip:
let
function = "net.ip.add";
delta' = typechecks.numeric function "delta" delta;
ip' = typechecks.ip function "ip" ip;
@ -56,7 +61,8 @@ lib: let
# and you just want to calculate the offset between them — a result
# like "0.0.0.10" is not very useful (which is what you would get
# from net.ip.subtract).
diff = minuend: subtrahend: let
diff = minuend: subtrahend:
let
function = "net.ip.diff";
minuend' = typechecks.ip function "minuend" minuend;
subtrahend' = typechecks.ip function "subtrahend" subtrahend;
@ -78,7 +84,8 @@ lib: let
# parameter. Its implementation takes the "delta" parameter,
# coerces it to be the same type as the "ip" paramter, negates it
# (using two's complement), and then adds it to "ip".
subtract = delta: ip: let
subtract = delta: ip:
let
function = "net.ip.subtract";
delta' = typechecks.numeric function "delta" delta;
ip' = typechecks.ip function "ip" ip;
@ -87,6 +94,7 @@ lib: let
};
mac = {
# add :: (ip | mac | integer) -> mac -> mac
#
# Examples:
@ -106,7 +114,8 @@ lib: let
# Adding MAC to MAC:
# > net.mac.add "fe:ed:fa:00:00:00" "00:00:00:ce:f0:0d"
# "fe:ed:fa:ce:f0:0d"
add = delta: mac: let
add = delta: mac:
let
function = "net.mac.add";
delta' = typechecks.numeric function "delta" delta;
mac' = typechecks.mac function "mac" mac;
@ -122,7 +131,8 @@ lib: let
#
# The difference between net.mac.diff and net.mac.subtract is that
# net.mac.diff will always return an integer.
diff = minuend: subtrahend: let
diff = minuend: subtrahend:
let
function = "net.mac.diff";
minuend' = typechecks.mac function "minuend" minuend;
subtrahend' = typechecks.mac function "subtrahend" subtrahend;
@ -138,7 +148,8 @@ lib: let
#
# The difference between net.mac.subtract and net.mac.diff is that
# net.mac.subtract will always return a MAC address.
subtract = delta: mac: let
subtract = delta: mac:
let
function = "net.mac.subtract";
delta' = typechecks.numeric function "delta" delta;
mac' = typechecks.mac function "mac" mac;
@ -154,7 +165,8 @@ lib: let
#
# > net.cidr.add (-2) "127.0.0.0/8"
# "125.0.0.0/8"
add = delta: cidr: let
add = delta: cidr:
let
function = "net.cidr.add";
delta' = typechecks.numeric function "delta" delta;
cidr' = typechecks.cidr function "cidr" cidr;
@ -168,7 +180,8 @@ lib: let
#
# > net.cidr.child "127.0.0.0/8" "10.0.0.0/8"
# false
child = subcidr: cidr: let
child = subcidr: cidr:
let
function = "net.cidr.child";
subcidr' = typechecks.cidr function "subcidr" subcidr;
cidr' = typechecks.cidr function "cidr" cidr;
@ -182,7 +195,8 @@ lib: let
#
# > net.cidr.contains "127.0.0.1" "192.168.0.0/16"
# false
contains = ip: cidr: let
contains = ip: cidr:
let
function = "net.cidr.contains";
ip' = typechecks.ip function "ip" ip;
cidr' = typechecks.cidr function "cidr" cidr;
@ -199,7 +213,8 @@ lib: let
#
# > net.cidr.capacity "dead:cafe:beef::/48" (saturates to maxBound)
# 9223372036854775807
capacity = cidr: let
capacity = cidr:
let
function = "net.cidr.capacity";
cidr' = typechecks.cidr function "cidr" cidr;
in
@ -218,7 +233,8 @@ lib: let
#
# Inpsired by:
# https://www.terraform.io/docs/configuration/functions/cidrhost.html
host = hostnum: cidr: let
host = hostnum: cidr:
let
function = "net.cidr.host";
hostnum' = typechecks.numeric function "hostnum" hostnum;
cidr' = typechecks.cidr function "cidr" cidr;
@ -232,7 +248,8 @@ lib: let
#
# > net.cidr.prefix "dead:cafe:beef::/48"
# 48
length = cidr: let
length = cidr:
let
function = "net.cidr.length";
cidr' = typechecks.cidr function "cidr" cidr;
in
@ -245,7 +262,8 @@ lib: let
#
# > net.cidr.make 40 "dead:cafe:beef::feed:face:f00d"
# "dead:cafe:be00::/40"
make = length: base: let
make = length: base:
let
function = "net.cidr.make";
length' = typechecks.int function "length" length;
base' = typechecks.ip function "base" base;
@ -259,7 +277,8 @@ lib: let
#
# > net.cidr.netmask "dead:cafe:beef::/64"
# "ffff:ffff:ffff:ffff::"
netmask = cidr: let
netmask = cidr:
let
function = "net.cidr.netmask";
cidr' = typechecks.cidr function "cidr" cidr;
in
@ -272,7 +291,8 @@ lib: let
#
# > net.cidr.prefix "dead:cafe:beef::/48"
# 80
size = cidr: let
size = cidr:
let
function = "net.cidr.size";
cidr' = typechecks.cidr function "cidr" cidr;
in
@ -291,26 +311,23 @@ lib: let
#
# Inspired by:
# https://www.terraform.io/docs/configuration/functions/cidrsubnet.html
subnet = length: netnum: cidr: let
subnet = length: netnum: cidr:
let
function = "net.cidr.subnet";
length' = typechecks.int function "length" length;
netnum' = typechecks.numeric function "netnum" netnum;
cidr' = typechecks.cidr function "cidr" cidr;
in
builders.cidr (implementations.cidr.subnet length' netnum' cidr');
};
}
// {
types = let
mkParsedOptionType = {
name,
description,
parser,
builder,
}: let
normalize = def:
def
// {
} // ({
types =
let
mkParsedOptionType = { name, description, parser, builder }:
let
normalize = def: def // {
value = builder (parser def.value);
};
in
@ -320,28 +337,31 @@ lib: let
merge = loc: defs: lib.mergeEqualOption loc (map normalize defs);
};
dependent-ip = type: cidr: let
dependent-ip = type: cidr:
let
cidrs =
if builtins.isList cidr
then cidr
else [ cidr ];
in
lib.types.addCheck type (i: lib.any (net.cidr.contains i) cidrs)
// {
lib.types.addCheck type (i: lib.any (net.cidr.contains i) cidrs) // {
description = type.description + " in ${builtins.concatStringsSep " or " cidrs}";
};
dependent-cidr = type: cidr: let
dependent-cidr = type: cidr:
let
cidrs =
if builtins.isList cidr
then cidr
else [ cidr ];
in
lib.types.addCheck type (i: lib.any (net.cidr.child i) cidrs)
// {
lib.types.addCheck type (i: lib.any (net.cidr.child i) cidrs) // {
description = type.description + " in ${builtins.concatStringsSep " or " cidrs}";
};
in rec {
in
rec {
ip = mkParsedOptionType {
name = "ip";
description = "IPv4 or IPv6 address";
@ -402,22 +422,23 @@ lib: let
parser = parsers.mac;
builder = builders.mac;
};
};
};
}
);
list = {
cons = a: b: [ a ] ++ b;
};
bit = let
bit =
let
shift = n: x:
if n < 0
then x * math.pow 2 (-n)
else let
safeDiv = n: d:
if d == 0
then 0
else n / d;
else
let
safeDiv = n: d: if d == 0 then 0 else n / d;
d = math.pow 2 n;
in
if x < 0
@ -437,7 +458,8 @@ lib: let
not = xor (-1);
mask = n: and (left n 1 - 1);
in {
in
{
inherit left right and or xor not mask;
};
@ -462,7 +484,9 @@ lib: let
else pow (x * x) (n / 2);
};
parsers = let
parsers =
let
# fmap :: (a -> b) -> parser a -> parser b
fmap = f: ma: bind ma (a: pure (f a));
@ -489,7 +513,8 @@ lib: let
empty = string: null;
# alt :: parser a -> parser a -> parser a
alt = left: right: string: let
alt = left: right: string:
let
result = left string;
in
if builtins.isNull result
@ -497,10 +522,7 @@ lib: let
else result;
# guard :: bool -> parser {}
guard = condition:
if condition
then pure {}
else empty;
guard = condition: if condition then pure { } else empty;
# mfilter :: (a -> bool) -> parser a -> parser a
mfilter = f: parser: bind parser (a: then_ (guard (f a)) (pure a));
@ -512,7 +534,8 @@ lib: let
many = v: alt (some v) (pure [ ]);
# bind :: parser a -> (a -> parser b) -> parser b
bind = parser: f: string: let
bind = parser: f: string:
let
a = parser string;
in
if builtins.isNull a
@ -520,7 +543,8 @@ lib: let
else f a.result a.leftovers;
# run :: parser a -> string -> maybe a
run = parser: string: let
run = parser: string:
let
result = parser string;
in
if builtins.isNull result || result.leftovers != ""
@ -536,14 +560,13 @@ lib: let
};
# Count how many characters were consumed by a parser
count = parser: string: let
count = parser: string:
let
result = parser string;
in
if builtins.isNull result
then null
else
result
// {
else result // {
result = {
inherit (result) result;
count = with result;
@ -564,16 +587,15 @@ lib: let
string = css:
if css == ""
then pure { }
else let
else
let
c = builtins.substring 0 1 css;
cs = builtins.substring 1 (-1) css;
in
then_ (char c) (string cs);
digit = set:
bind next (
c:
then_
digit = set: bind next (
c: then_
(guard (builtins.hasAttr c set))
(pure (builtins.getAttr c set))
);
@ -591,9 +613,7 @@ lib: let
"9" = 9;
};
hexadecimalDigits =
decimalDigits
// {
hexadecimalDigits = decimalDigits // {
"a" = 10;
"b" = 11;
"c" = 12;
@ -624,7 +644,8 @@ lib: let
hexadecimal = fmap fromHexadecimalDigits (some (digit hexadecimalDigits));
ipv4 = let
ipv4 =
let
dot = char ".";
octet = mfilter (n: n < 256) decimal;
@ -639,7 +660,8 @@ lib: let
# This is more or less a literal translation of
# https://hackage.haskell.org/package/ip/docs/src/Net.IPv6.html#parser
ipv6 = let
ipv6 =
let
colon = char ":";
hextet = limit 4 hexadecimal;
@ -647,7 +669,8 @@ lib: let
fromHextets = hextets:
if builtins.length hextets != 8
then empty
else let
else
let
a = builtins.elemAt hextets 0;
b = builtins.elemAt hextets 1;
c = builtins.elemAt hextets 2;
@ -666,20 +689,21 @@ lib: let
};
};
ipv4' =
fmap
ipv4' = fmap
(
address: let
address:
let
upper = bit.right 16 address.ipv4;
lower = bit.mask 16 address.ipv4;
in [upper lower]
in
[ upper lower ]
)
ipv4;
part = n: let
part = n:
let
n' = n + 1;
hex =
liftA2 list.cons hextet
hex = liftA2 list.cons hextet
(
then_ colon
(
@ -691,13 +715,15 @@ lib: let
in
if n == 7
then fmap (a: [ a ]) hextet
else if n == 6
else
if n == 6
then alt ipv4' hex
else hex;
doubleColon = n:
bind (alt afterDoubleColon (pure [ ])) (
rest: let
rest:
let
missing = 8 - n - builtins.length rest;
in
if missing < 0
@ -715,6 +741,7 @@ lib: let
(pure [ ])
)
);
in
bind
(
@ -740,7 +767,8 @@ lib: let
ipv6
(then_ (char "/") (mfilter (n: n <= 128) decimal));
mac = let
mac =
let
colon = char ":";
octet = exactly 2 hexadecimal;
@ -752,7 +780,9 @@ lib: let
};
in
liftA6 fromOctets octet octet' octet' octet' octet' octet';
in {
in
{
ipv4 = run ipv4;
ipv6 = run ipv6;
ip = run (alt ipv4 ipv6);
@ -763,8 +793,11 @@ lib: let
numeric = run (alt (alt ipv4 ipv6) mac);
};
builders = let
ipv4 = address: let
builders =
let
ipv4 = address:
let
abcd = address.ipv4;
abc = bit.right 8 abcd;
ab = bit.right 8 abc;
@ -777,23 +810,31 @@ lib: let
# This is more or less a literal translation of
# https://hackage.haskell.org/package/ip/docs/src/Net.IPv6.html#encode
ipv6 = address: let
ipv6 = address:
let
digits = "0123456789abcdef";
toHexString = n: let
toHexString = n:
let
rest = bit.right 4 n;
current = bit.mask 4 n;
prefix =
if rest == 0
then ""
else toHexString rest;
in "${prefix}${builtins.substring current 1 digits}";
in
"${prefix}${builtins.substring current 1 digits}";
in
if (with address.ipv6; a == 0 && b == 0 && c == 0 && d > 65535)
then "::${ipv4 { ipv4 = address.ipv6.d; }}"
else if (with address.ipv6; a == 0 && b == 0 && c == 65535)
else
if (with address.ipv6; a == 0 && b == 0 && c == 65535)
then "::ffff:${ipv4 { ipv4 = address.ipv6.d; }}"
else let
else
let
a = bit.right 16 address.ipv6.a;
b = bit.mask 16 address.ipv6.a;
c = bit.right 16 address.ipv6.b;
@ -807,10 +848,12 @@ lib: let
# calculate the position and size of the longest sequence of
# zeroes within the list of hextets
longest = let
longest =
let
go = i: current: best:
if i < builtins.length hextets
then let
then
let
n = builtins.elemAt hextets i;
current' =
@ -821,9 +864,7 @@ lib: let
size = 1;
position = i;
}
else
current
// {
else current // {
size = current.size + 1;
}
else null;
@ -833,7 +874,8 @@ lib: let
then
if builtins.isNull best
then current'
else if current'.size > best.size
else
if current'.size > best.size
then current'
else best
else best;
@ -848,7 +890,8 @@ lib: let
in
if builtins.isNull longest
then format hextets
else let
else
let
sublist = i: length: xs:
map
(builtins.elemAt xs)
@ -859,40 +902,52 @@ lib: let
before = sublist 0 longest.position hextets;
after = sublist end (builtins.length hextets - end) hextets;
in "${format before}::${format after}";
in
"${format before}::${format after}";
ip = address:
if address ? ipv4
then ipv4 address
else ipv6 address;
cidrv4 = cidr: "${ipv4 cidr.base}/${toString cidr.length}";
cidrv4 = cidr:
"${ipv4 cidr.base}/${toString cidr.length}";
cidrv6 = cidr: "${ipv6 cidr.base}/${toString cidr.length}";
cidrv6 = cidr:
"${ipv6 cidr.base}/${toString cidr.length}";
cidr = cidr: "${ip cidr.base}/${toString cidr.length}";
cidr = cidr:
"${ip cidr.base}/${toString cidr.length}";
mac = address: let
mac = address:
let
digits = "0123456789abcdef";
octet = n: let
octet = n:
let
upper = bit.right 4 n;
lower = bit.mask 4 n;
in "${builtins.substring upper 1 digits}${builtins.substring lower 1 digits}";
in let
in
"${builtins.substring upper 1 digits}${builtins.substring lower 1 digits}";
in
let
a = bit.mask 8 (bit.right 40 address.mac);
b = bit.mask 8 (bit.right 32 address.mac);
c = bit.mask 8 (bit.right 24 address.mac);
d = bit.mask 8 (bit.right 16 address.mac);
e = bit.mask 8 (bit.right 8 address.mac);
f = bit.mask 8 (bit.right 0 address.mac);
in "${octet a}:${octet b}:${octet c}:${octet d}:${octet e}:${octet f}";
in {
in
"${octet a}:${octet b}:${octet c}:${octet d}:${octet e}:${octet f}";
in
{
inherit ipv4 ipv6 ip cidrv4 cidrv6 cidr mac;
};
arithmetic = rec {
# or :: (ip | mac | integer) -> (ip | mac | integer) -> (ip | mac | integer)
or = a_: b: let
or = a_: b:
let
a = coerce b a_;
in
if a ? ipv6
@ -915,7 +970,8 @@ lib: let
else bit.or a b;
# and :: (ip | mac | integer) -> (ip | mac | integer) -> (ip | mac | integer)
and = a_: b: let
and = a_: b:
let
a = coerce b a_;
in
if a ? ipv6
@ -959,22 +1015,26 @@ lib: let
else bit.not a;
# add :: (ip | mac | integer) -> (ip | mac | integer) -> (ip | mac | integer)
add = let
add =
let
split = a: {
fst = bit.mask 32 (bit.right 32 a);
snd = bit.mask 32 a;
};
in
a_: b: let
a_: b:
let
a = coerce b a_;
in
if a ? ipv6
then let
then
let
a' = split (a.ipv6.a + b.ipv6.a + b'.fst);
b' = split (a.ipv6.b + b.ipv6.b + c'.fst);
c' = split (a.ipv6.c + b.ipv6.c + d'.fst);
d' = split (a.ipv6.d + b.ipv6.d);
in {
in
{
ipv6 = {
a = a'.snd;
b = b'.snd;
@ -996,8 +1056,9 @@ lib: let
subtract = a: b: add (add 1 (not (coerce b a))) b;
# diff :: (ip | mac | integer) -> (ip | mac | integer) -> (ipv6 | integer)
diff = a: b: let
toIPv6 = coerce {ipv6.a = 0;};
diff = a: b:
let
toIPv6 = coerce ({ ipv6.a = 0; });
result = (subtract b (toIPv6 a)).ipv6;
max32 = bit.left 32 1 - 1;
in
@ -1011,7 +1072,8 @@ lib: let
left = i: right (-i);
# right :: integer -> (ip | mac | integer) -> (ip | mac | integer)
right = let
right =
let
step = i: x: {
_1 = bit.mask 32 (bit.right (i + 96) x);
_2 = bit.mask 32 (bit.right (i + 64) x);
@ -1025,12 +1087,14 @@ lib: let
in
i: x:
if x ? ipv6
then let
then
let
a' = step i x.ipv6.a;
b' = step i x.ipv6.b;
c' = step i x.ipv6.c;
d' = step i x.ipv6.d;
in {
in
{
ipv6 = {
a = ors [ a'._4 b'._3 c'._2 d'._1 ];
b = ors [ a'._5 b'._4 c'._3 d'._2 ];
@ -1116,7 +1180,8 @@ lib: let
else {
mac = bit.mask 48 value;
}
else if value ? ipv6
else
if value ? ipv6
then
builtins.foldl' bit.or 0
[
@ -1157,15 +1222,18 @@ lib: let
cidr = rec {
# add :: (ip | mac | integer) -> cidr -> cidr
add = delta: cidr: let
add = delta: cidr:
let
size' = size cidr;
in {
in
{
base = arithmetic.left size' (arithmetic.add delta (arithmetic.right size' cidr.base));
inherit (cidr) length;
};
# capacity :: cidr -> integer
capacity = cidr: let
capacity = cidr:
let
size' = size cidr;
in
if size' > 62
@ -1180,7 +1248,8 @@ lib: let
contains = ip: cidr: host 0 (make cidr.length ip) == host 0 cidr;
# host :: (ip | mac | integer) -> cidr -> ip
host = index: cidr: let
host = index: cidr:
let
index' = arithmetic.coerce cidr.base index;
in
arithmetic.or (arithmetic.shadow cidr.length index') cidr.base;
@ -1192,60 +1261,51 @@ lib: let
netmask = cidr: arithmetic.coshadow cidr.length (arithmetic.coerce cidr.base (-1));
# size :: cidr -> integer
size = cidr:
(
if cidr.base ? ipv6
then 128
else 32
)
- cidr.length;
size = cidr: (if cidr.base ? ipv6 then 128 else 32) - cidr.length;
# subnet :: integer -> (ip | mac | integer) -> cidr -> cidr
subnet = length: index: cidr: let
subnet = length: index: cidr:
let
length' = cidr.length + length;
index' = arithmetic.coerce cidr.base index;
size =
(
if cidr.base ? ipv6
then 128
else 32
)
- length';
size = (if cidr.base ? ipv6 then 128 else 32) - length';
in
make length' (host (arithmetic.left size index') cidr);
# make :: integer -> ip -> cidr
make = length: base: let
length' =
math.clamp 0 (
if base ? ipv6
then 128
else 32
)
length;
in {
make = length: base:
let
length' = math.clamp 0 (if base ? ipv6 then 128 else 32) length;
in
{
base = arithmetic.coshadow length' base;
length = length';
};
};
};
typechecks = let
typechecks =
let
fail = description: function: argument:
builtins.throw "${function}: ${argument} parameter must be ${description}";
meta = parser: description: function: argument: input: let
meta = parser: description: function: argument: input:
let
error = fail description function argument;
in
if !builtins.isString input
then error
else let
else
let
result = parser input;
in
if builtins.isNull result
then error
else result;
in {
in
{
int = function: argument: input:
if builtins.isInt input
then input
@ -1258,5 +1318,6 @@ lib: let
then input
else meta parsers.numeric "an integer or IPv4, IPv6 or MAC address" function argument input;
};
in
net