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

@ -133,10 +133,7 @@
};
};
outputs = inputs: let
inherit (inputs) self;
inherit (self) outputs;
in
outputs = inputs:
inputs.flake-parts.lib.mkFlake { inherit inputs; } ({ withSystem, flake-parts-lib, ... }: {
systems = [
"aarch64-linux"
@ -147,12 +144,12 @@
];
imports = [
./nix/pkgs.nix
./nix/modules.nix
./nix/configurations.nix
./nix/agenix.nix
./nix/deploy.nix
./nix/topology
./modules/flake/pkgs.nix
./modules/flake/modules.nix
./modules/flake/configurations.nix
./modules/flake/agenix.nix
./modules/flake/deploy.nix
./modules/flake/topology
];
perSystem = { lib, pkgs, system, ... }: {
@ -170,10 +167,14 @@
};
flake = {
inherit self;
inherit (inputs) self;
# Automatic modules, see `./modules/flake/modules.nix`
autoModules.enableAll = true;
# Automatic configurations, see `./modules/flake/configurations.nix`
autoConfigurations.enableAll = true;
# Templates
templates = import ./templates {
inherit inputs;

View file

@ -0,0 +1,299 @@
{ lib, config, self, inputs, withSystem, ... }:
let
outputs = self;
inherit (import ../../nix/utils.nix { inherit lib self; })
and
hasFiles
hasDirectories
recurseDir
configuration-type-to-outputs-machines;
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 = "${root}/home.nix";
} else {
# Not nixOnDroid
users = lib.attrsets.genAttrs
users
(user: import "${root}/home/${user}.nix");
});
};
# Configuration helpers
configurationTypes = ["nixos" "nix-on-droid" "nix-darwin" "home-manager"];
mkNixosHost = args @ { root, system, hostname, users }: lib.nixosSystem {
inherit system;
pkgs = withSystem system ({ pkgs, ... }: pkgs);
modules = [
# Main configuration
"${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
"${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
"${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 = [
"${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
{
options = let
inherit (lib) types;
in {
flake.autoConfigurations = lib.mkOption {
description = ''
Automagically generate configurations from walking directories with Nix files
'';
type = types.submodule (submodule: {
options = {
enableAll = lib.mkEnableOption "Automatic ${builtins.toString configurationTypes} configurations extraction";
baseDir = lib.mkOption {
description = ''
Base directory of the contained configurations, used as a base for the rest of the options
'';
type = types.path;
default = "${self}/machines";
defaultText = ''''${self}/machines'';
};
} // (
lib.pipe
configurationTypes
[
(builtins.map
# NOTE: create small submodule for every `configurationType`
(configurationType:
lib.nameValuePair
"${configurationType}"
(lib.mkOption {
type = types.submodule {
options = {
# NOTE: each can be enabled (default global `enableAll`)
enable = lib.mkEnableOption "Automatic ${configurationType} configurations extraction" // {
default = submodule.config.enableAll;
};
# NOTE: each can be read from a different directory
# (default global `baseDir` + `camelToKebab`-ed `configurationType`)
dir = lib.mkOption {
type = types.path;
default = "${submodule.config.baseDir}/${configurationType}";
};
};
};
default = {};
})))
builtins.listToAttrs
]);
});
default = {};
};
};
config = {
flake = let
autoMachines =
lib.pipe
configurationTypes
[
(builtins.map
(configurationType:
lib.nameValuePair
"${configuration-type-to-outputs-machines configurationType}"
(if config.flake.autoConfigurations.${configurationType}.enable
then recurseDir config.flake.autoConfigurations.${configurationType}.dir
else { })))
builtins.listToAttrs
];
in {
# Machines
inherit (autoMachines)
nixosMachines
nixDarwinMachines
nixOnDroidMachines
homeManagerMachines;
# Configurations
nixosConfigurations =
createConfigurations
({ system, host, configurationFiles, ... }:
and
[
(hasFiles
[ "configuration.nix" ]
configurationFiles)
# (hasDirectories
# [ "home" ]
# config)
])
({ system, host, configurationFiles, ... }:
mkNixosHost {
root = "${config.flake.autoConfigurations.nixos.dir}/${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 = "${config.flake.autoConfigurations.nix-on-droid.dir}/${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 = "${config.flake.autoConfigurations.nix-darwin.dir}/${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 = "${config.flake.homeManager.home-manager.dir}/${system}/${host}";
inherit system;
hostname = host;
})
config.flake.homeManagerMachines;
};
};
}

View file

@ -1,7 +1,7 @@
{ lib, config, self, inputs, ... }:
let
inherit (import ./utils.nix { inherit lib self; })
inherit (import ../../nix/utils.nix { inherit lib self; })
accumulateMachines
configuration-type-to-deploy-type;
in
@ -14,7 +14,7 @@ in
({ host, system, configuration-type, configuration }:
let
deploy-config-path =
../machines/${configuration-type}/${system}/${host}/deploy.nix;
"${config.flake.autoConfigurations.${configuration-type}.dir}/${system}/${host}/deploy.nix";
deploy-config =
import deploy-config-path;
in

View file

@ -2,15 +2,16 @@
let
outputs = self;
inherit (import ./utils.nix { inherit lib self; })
inherit (import ../../nix/utils.nix { inherit lib self; })
eq
and
hasFiles
camelToKebab;
configuration-type-to-outputs-modules;
in
let
# Modules helpers
moduleTypes = ["nixos" "nixOnDroid" "nixDarwin" "homeManager" "flake"];
moduleTypes = ["nixos" "nix-on-droid" "nix-darwin" "home-manager" "flake"];
createModules = baseDir: { passthru ? { inherit inputs outputs; }, ... }:
lib.pipe baseDir [
# Read given directory
@ -68,7 +69,7 @@ in
in {
flake.autoModules = lib.mkOption {
description = ''
Automagivally generate modules from walking directories with Nix files
Automagically generate modules from walking directories with Nix files
'';
type = types.submodule (submodule: {
options = {
@ -101,7 +102,7 @@ in
# (default global `baseDir` + `camelToKebab`-ed `moduleType`)
dir = lib.mkOption {
type = types.path;
default = "${submodule.config.baseDir}/${camelToKebab moduleType}";
default = "${submodule.config.baseDir}/${moduleType}";
};
};
};
@ -123,7 +124,7 @@ in
(builtins.map
(moduleType:
lib.nameValuePair
"${moduleType}Modules"
"${configuration-type-to-outputs-modules moduleType}"
(if config.flake.autoModules.${moduleType}.enable
then createModules config.flake.autoModules.${moduleType}.dir { }
else { })))

View file

@ -23,7 +23,8 @@
-shave 1x1 \
$out
'';
images.TL-WR740N = removebg {
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";
@ -31,7 +32,7 @@
};
fuzz = 15;
};
images.ZBT-WR8305RT = removebg {
ZBT-WR8305RT = removebg {
image = pkgs.fetchurl {
name = "ZBT-WR8305RT.jpg";
url = "https://vseplus.com/images/p/full/213140a.jpg";
@ -39,7 +40,7 @@
};
fuzz = 2;
};
images.cheetah = removebg {
cheetah = removebg {
image = pkgs.fetchurl {
name = "cheetah.jpg";
url = "https://m.media-amazon.com/images/I/51OFxuD1GgL._AC_SL1000_.jpg";
@ -47,8 +48,13 @@
};
fuzz = 20;
};
};
in {
topology = {
# nixosConfigurations = {
# inherit (self.nixosConfigurations)
# jeeves;
# };
nixosConfigurations = self.nixosConfigurations;
modules = [
({ config, ... }: let

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,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

@ -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
{