feat(flake)!: *dogfood* on all flake modules

Export all `./nix/` flake modules
Modularize `configurations` on `machines` dir
Also move `toposort` images to a single attrset binding
This commit is contained in:
reo101 2024-07-25 01:25:21 +03:00
parent c6432c7dab
commit 91f0deb3bc
Signed by: reo101
GPG key ID: 675AA7EF13964ACB
10 changed files with 363 additions and 284 deletions

View file

@ -1,13 +0,0 @@
{ lib, config, self, inputs, ... }:
{
imports = [
inputs.agenix-rekey.flakeModule
];
perSystem = {
agenix-rekey = {
nodes = self.nixosConfigurations;
};
};
}

View file

@ -1,225 +0,0 @@
{ lib, config, self, inputs, withSystem, ... }:
let
outputs = self;
inherit (import ./utils.nix { inherit lib self; })
and
hasFiles
hasDirectories;
in
let
homeManagerModule = { root, system, hostname, users ? null }: {
home-manager = {
# Use same `pkgs` instance as system (i.e. carry over overlays)
useGlobalPkgs = true;
# Do not keep packages in ${HOME}
useUserPackages = true;
# Default import all of our exported homeManagerModules
sharedModules = builtins.attrValues config.flake.homeManagerModules;
# Pass in `inputs`, `outputs` and maybe `meta`
extraSpecialArgs = {
inherit inputs outputs;
# TODO: meta?
inherit hostname;
};
} // (if users == null then {
# nixOnDroid
config = (lib.path.append root "home.nix");
} else {
# Not nixOnDroid
users = lib.attrsets.genAttrs
users
(user: import (lib.path.append root "home/${user}.nix"));
});
};
# Configuration helpers
mkNixosHost = args @ { root, system, hostname, users }: lib.nixosSystem {
inherit system;
pkgs = withSystem system ({ pkgs, ... }: pkgs);
modules = [
# Main configuration
(lib.path.append root "configuration.nix")
# Home Manager
inputs.home-manager.nixosModules.home-manager
(homeManagerModule args)
# (r)agenix && agenix-rekey
inputs.ragenix.nixosModules.default
inputs.agenix-rekey.nixosModules.default
# nix-topology
inputs.nix-topology.nixosModules.default
# Sane default `networking.hostName`
{
networking.hostName = lib.mkDefault hostname;
}
] ++ (builtins.attrValues config.flake.nixosModules);
specialArgs = {
inherit inputs outputs;
};
};
mkNixOnDroidHost = args @ { root, system, hostname }: inputs.nix-on-droid.lib.nixOnDroidConfiguration {
# NOTE: inferred by `pkgs.system`
# inherit system;
pkgs = withSystem system ({ pkgs, ... }: pkgs);
modules = [
# Main configuration
(lib.path.append root "configuration.nix")
# Home Manager
(homeManagerModule args)
] ++ (builtins.attrValues config.flake.nixOnDroidModules);
extraSpecialArgs = {
inherit inputs outputs;
};
home-manager-path = inputs.home-manager.outPath;
};
mkNixDarwinHost = args @ { root, system, hostname, users }: inputs.nix-darwin.lib.darwinSystem {
inherit system;
pkgs = withSystem system ({ pkgs, ... }: pkgs);
modules = [
# Main configuration
(lib.path.append root "configuration.nix")
# Home Manager
inputs.home-manager.darwinModules.home-manager
(homeManagerModule args)
# # Set `nixpkgs.hostPlatform`
# {
# nixpkgs.hostPlatform = system;
# }
] ++ (builtins.attrValues config.flake.nixDarwinModules);
specialArgs = {
inherit inputs outputs;
};
};
mkHomeManagerHost = args @ { root, system, hostname }: inputs.home-manager.lib.homeManagerConfiguration {
inherit system;
pkgs = withSystem system ({ pkgs, ... }: pkgs);
modules = [
(lib.path.append root "home.nix")
] ++ (builtins.attrValues config.flake.homeManagerModules);
extraSpecialArgs = {
inherit inputs outputs;
inherit hostname;
};
};
createConfigurations =
pred: mkHost: machines:
lib.foldAttrs
lib.const
[ ]
(builtins.attrValues
(builtins.mapAttrs
(system: hosts:
lib.concatMapAttrs
(host: configurationFiles:
lib.optionalAttrs
(and [
(host != "__template__")
(pred { inherit system host configurationFiles; })
])
{
${host} = mkHost { inherit system host configurationFiles; };
})
hosts)
machines));
in
{
imports = [
./machines.nix
];
flake = {
# Configurations
nixosConfigurations =
createConfigurations
({ system, host, configurationFiles, ... }:
and
[
(hasFiles
[ "configuration.nix" ]
configurationFiles)
# (hasDirectories
# [ "home" ]
# config)
])
({ system, host, configurationFiles, ... }:
mkNixosHost {
root = ../machines/nixos/${system}/${host};
inherit system;
hostname = host;
users = (builtins.map
(lib.strings.removeSuffix ".nix")
(builtins.attrNames (configurationFiles."home" or { })));
})
config.flake.nixosMachines;
nixOnDroidConfigurations =
createConfigurations
({ system, host, configurationFiles, ... }:
and
[
(hasFiles
[ "configuration.nix" "home.nix" ]
configurationFiles)
])
({ system, host, configurationFiles, ... }:
mkNixOnDroidHost {
root = ../machines/nix-on-droid/${system}/${host};
inherit system;
hostname = host;
})
config.flake.nixOnDroidMachines;
darwinConfigurations =
createConfigurations
({ system, host, configurationFiles, ... }:
and
[
(hasFiles
[ "configuration.nix" ]
configurationFiles)
(hasDirectories
[ "home" ]
configurationFiles)
])
({ system, host, configurationFiles, ... }:
mkNixDarwinHost {
root = ../machines/nix-darwin/${system}/${host};
inherit system;
hostname = host;
users = (builtins.map
(lib.strings.removeSuffix ".nix")
(builtins.attrNames (configurationFiles."home" or { })));
})
config.flake.nixDarwinMachines;
homeConfigurations =
createConfigurations
({ system, host, configurationFiles, ... }:
and
[
(hasFiles
[ "home.nix" ]
configurationFiles)
])
({ system, host, configurationFiles, ... }:
mkHomeManagerHost {
root = ../machines/home-manager/${system}/${host};
inherit system;
hostname = host;
})
config.flake.homeManagerMachines;
};
}

View file

@ -1,45 +0,0 @@
{ lib, config, self, inputs, ... }:
let
inherit (import ./utils.nix { inherit lib self; })
accumulateMachines
configuration-type-to-deploy-type;
in
{
flake = {
deploy.nodes =
accumulateMachines
# TODO: nix-on-droid
["nixos" "nix-darwin"]
({ host, system, configuration-type, configuration }:
let
deploy-config-path =
../machines/${configuration-type}/${system}/${host}/deploy.nix;
deploy-config =
import deploy-config-path;
in
lib.optionalAttrs
(builtins.pathExists deploy-config-path)
{
${host} = {
inherit (deploy-config)
hostname;
profiles.system = deploy-config // {
path =
let
deploy-type = configuration-type-to-deploy-type configuration-type;
in
inputs.deploy-rs.lib.${system}.activate.${deploy-type} configuration;
};
};
}
);
checks =
lib.mapAttrs
(system: deployLib:
deployLib.deployChecks
self.deploy)
inputs.deploy-rs.lib;
};
}

View file

@ -1,18 +0,0 @@
{ lib, config, self, inputs, ... }:
let
inherit (import ./utils.nix { inherit lib self; })
recurseDir;
in
let
machines = recurseDir ../machines;
in
{
flake = {
# Machines
nixosMachines = machines.nixos or { };
nixDarwinMachines = machines.nix-darwin or { };
nixOnDroidMachines = machines.nix-on-droid or { };
homeManagerMachines = machines.home-manager or { };
};
}

View file

@ -1,143 +0,0 @@
{ lib, config, self, inputs, ... }:
let
outputs = self;
inherit (import ./utils.nix { inherit lib self; })
eq
and
hasFiles
camelToKebab;
in
let
# Modules helpers
moduleTypes = ["nixos" "nixOnDroid" "nixDarwin" "homeManager" "flake"];
createModules = baseDir: { passthru ? { inherit inputs outputs; }, ... }:
lib.pipe baseDir [
# Read given directory
builtins.readDir
# Map each entry to a module
(lib.mapAttrs'
(name: type:
let
# BUG: cannot use `append` because of `${self}` (not a path)
# moduleDir = lib.path.append baseDir "${name}";
moduleDir = "${baseDir}/${name}";
in
if and [
(type == "directory")
(hasFiles [ "default.nix" ] (builtins.readDir moduleDir))
] then
# Classic module in a directory
lib.nameValuePair
name
(import moduleDir)
else if and [
(type == "regular")
(lib.hasSuffix ".nix" name)
] then
# Classic module in a file
lib.nameValuePair
(lib.removeSuffix ".nix" name)
(import moduleDir)
else
# Invalid module
lib.nameValuePair
name
null))
# Filter invalid modules
(lib.filterAttrs
(moduleName: module:
module != null))
# Passthru if needed
(lib.mapAttrs
(moduleName: module:
if and [
(builtins.isFunction
module)
(eq
(lib.pipe module [ builtins.functionArgs builtins.attrNames ])
(lib.pipe passthru [ builtins.attrNames ]))
]
then module passthru
else module))
];
in
{
options = let
inherit (lib) types;
in {
flake.autoModules = lib.mkOption {
description = ''
Automagivally generate modules from walking directories with Nix files
'';
type = types.submodule (submodule: {
options = {
enableAll = lib.mkEnableOption "Automatic ${builtins.toString moduleTypes} modules extraction";
baseDir = lib.mkOption {
description = ''
Base directory of the contained modules, used as a base for the rest of the options
'';
type = types.path;
default = "${self}/modules";
defaultText = ''''${self}/modules'';
};
} // (
lib.pipe
moduleTypes
[
(builtins.map
# NOTE: create small submodule for every `moduleType`
(moduleType:
lib.nameValuePair
"${moduleType}"
(lib.mkOption {
type = types.submodule {
options = {
# NOTE: each can be enabled (default global `enableAll`)
enable = lib.mkEnableOption "Automatic ${moduleType} modules extraction" // {
default = submodule.config.enableAll;
};
# NOTE: each can be read from a different directory
# (default global `baseDir` + `camelToKebab`-ed `moduleType`)
dir = lib.mkOption {
type = types.path;
default = "${submodule.config.baseDir}/${camelToKebab moduleType}";
};
};
};
default = {};
})))
builtins.listToAttrs
]);
});
default = {};
};
};
config = {
flake = let
autoModules =
lib.pipe
moduleTypes
[
(builtins.map
(moduleType:
lib.nameValuePair
"${moduleType}Modules"
(if config.flake.autoModules.${moduleType}.enable
then createModules config.flake.autoModules.${moduleType}.dir { }
else { })))
builtins.listToAttrs
];
in {
# NOTE: manually inheriting generated modules to avoid recursion
# (`autoModules` depends on `config.flake` itself)
inherit (autoModules)
nixosModules
nixOnDroidModules
nixDarwinModules
homeManagerModules
flakeModules;
};
};
}

View file

@ -1,21 +0,0 @@
{ inputs, self, lib, config, ... }:
{
perSystem = { system, ... }: {
_module.args.pkgs = import inputs.nixpkgs {
inherit system;
overlays = lib.attrValues self.overlays ++ [
inputs.neovim-nightly-overlay.overlays.default
inputs.zig-overlay.overlays.default
inputs.nix-topology.overlays.default
inputs.wired.overlays.default
# nix-on-droid overlay (needed for `proot`)
inputs.nix-on-droid.overlays.default
];
config = {
# TODO: per machine?
allowUnfree = true;
};
};
};
}

View file

@ -1,155 +0,0 @@
{ lib, config, self, inputs, ... }:
{
imports = [
inputs.nix-topology.flakeModule
];
perSystem = { lib, pkgs, system, ... }: let
removebg = { image, fuzz ? 10, ... }:
pkgs.runCommand "${image.name}.png" {
buildInputs = [
pkgs.imagemagick
];
} ''
magick ${image} \
-bordercolor white \
-border 1x1 \
-alpha set \
-channel RGBA \
-fuzz ${toString fuzz}% \
-fill none \
-floodfill +0+0 white \
-shave 1x1 \
$out
'';
images.TL-WR740N = removebg {
image = pkgs.fetchurl {
name = "TL-WR740N.jpg";
url = "https://static.tp-link.com/res/images/products/TL-WR740N_un_V6_1068_large_2_20150807163606.jpg";
hash = "sha256-/NpnnDh2V015lc3TGzez9eS8rINFtzVbCdN7d85NOt4=";
};
fuzz = 15;
};
images.ZBT-WR8305RT = removebg {
image = pkgs.fetchurl {
name = "ZBT-WR8305RT.jpg";
url = "https://vseplus.com/images/p/full/213140a.jpg";
hash = "sha256-ftTuXaBm99n+y+6fpRf0i63ykDx6xoJgwsQFpu2fNy4=";
};
fuzz = 2;
};
images.cheetah = removebg {
image = pkgs.fetchurl {
name = "cheetah.jpg";
url = "https://m.media-amazon.com/images/I/51OFxuD1GgL._AC_SL1000_.jpg";
hash = "sha256-Lvylh1geh81FZpqK1shj108M217zobWRgR4mEfbvKrc=";
};
fuzz = 20;
};
in {
topology = {
nixosConfigurations = self.nixosConfigurations;
modules = [
({ config, ... }: let
inherit (config.lib.topology)
mkInternet
mkRouter
mkSwitch
mkConnection
mkConnectionRev
;
in {
nodes.internet = mkInternet {
connections = mkConnection "router1" "eth1";
};
nodes.router1 = mkRouter "router1" {
info = "TP-Link TL-WR740N";
image = images.TL-WR740N;
interfaceGroups = [
["eth1" "eth2" "eth3" "eth4"]
["wan1"]
];
};
networks.router1 = {
name = "router1";
cidrv4 = "192.168.0.0/24";
style = {
primaryColor = "#b87f0d";
secondaryColor = null;
# one of "solid", "dashed", "dotted"
pattern = "solid";
};
};
nodes.router2 = mkRouter "router2" {
info = "Zbtlink ZBT-WR8305RT";
image = images.ZBT-WR8305RT;
interfaceGroups = [
["eth0"]
["lan1" "lan2" "lan3" "lan4"]
["wan"]
];
interfaces.eth0 = {
addresses = ["192.168.0.101"];
network = "router1";
physicalConnections = [(mkConnectionRev "router1" "eth2")];
};
};
networks.router2 = {
name = "router2";
cidrv4 = "192.168.1.0/24";
style = {
primaryColor = "#0dd62e";
secondaryColor = null;
pattern = "solid";
};
};
nodes.jeeves = {
interfaces.eth0 = {
addresses = ["192.168.1.210"];
network = "router2";
physicalConnections = [(mkConnectionRev "router2" "lan3")];
};
interfaces.wan0 = {
icon = "interfaces.wifi";
addresses = ["192.168.1.123"];
network = "router2";
physicalConnections = [(mkConnectionRev "router2" "wan")];
};
};
networks.wg0 = {
name = "wg0";
cidrv4 = "10.100.0.0/24";
style = {
primaryColor = "#ff0000";
secondaryColor = null;
pattern = "solid";
};
};
nodes.cheetah = {
deviceType = "device";
hardware = {
info = "Goole Pixel 7 Pro (cheetah)";
image = images.cheetah;
};
interfaces.wlan0 = {
icon = "interfaces.wifi";
addresses = ["192.168.1.240"];
network = "router2";
physicalConnections = [(mkConnectionRev "router2" "wan")];
};
interfaces.jeeves = {
addresses = ["10.100.0.2"];
network = "wg0";
physicalConnections = [(mkConnectionRev "jeeves" "wg0")];
};
};
})
];
};
};
}

View file

@ -34,7 +34,7 @@ rec {
lib.stringAsChars
(c: if c == lib.toUpper c then "-${lib.toLower c}" else c);
# NOTE: from Tweag's Nix Hour 76 - <https://github.com/tweag/nix-hour/blob/c4fd0f2fc3059f057571bbfd74f3c5e4021f526c/code/76/default.nix#L4-L22>
# NOTE: from Tweag's Nix Hour 76 - <https://github.com/tweag/nix-hour/blob/c4fd0f2fc3059f057571bbfd74f3c5e4021f526c/code/76/default.nix#L4-L22>
mutFirstChar =
f: s:
let
@ -55,18 +55,33 @@ rec {
(builtins.throw
(mkError configuration-type));
# TODO: abstract away `_Machines` and `_Modules`
configuration-type-to-outputs-machines =
gen-configuration-type-to
{
nixos = "nixosMachines";
nix-on-droid = "nixOnDroidMachines";
nix-darwin = "nixDarwinMachines";
home-manager = "homeMachines";
home-manager = "homeManagerMachines";
}
(configuration-type:
builtins.throw
"Invaild configuration-type \"${configuration-type}\" for flake outputs' machines");
configuration-type-to-outputs-modules =
gen-configuration-type-to
{
nixos = "nixosModules";
nix-on-droid = "nixOnDroidModules";
nix-darwin = "nixDarwinModules";
home-manager = "homeManagerModules";
flake = "flakeModules";
}
(configuration-type:
builtins.throw
"Invaild configuration-type \"${configuration-type}\" for flake outputs' modules");
configuration-type-to-outputs-configurations =
gen-configuration-type-to
{