2023-02-17 01:42:11 +01:00
|
|
|
{ inputs, outputs, ... }:
|
2023-02-14 10:10:08 +01:00
|
|
|
|
|
|
|
let
|
|
|
|
inherit (inputs) nixpkgs;
|
2023-02-17 01:42:11 +01:00
|
|
|
inherit (nixpkgs) lib;
|
2023-02-14 10:10:08 +01:00
|
|
|
in
|
|
|
|
rec {
|
2023-07-25 09:32:30 +02:00
|
|
|
# Boolean helpers
|
|
|
|
and = lib.all lib.id;
|
2023-07-29 17:12:22 +02:00
|
|
|
or = lib.any lib.id;
|
|
|
|
eq = x: y: x == y;
|
2023-07-25 09:32:30 +02:00
|
|
|
|
|
|
|
# Directory walking helpers
|
2023-02-14 10:10:08 +01:00
|
|
|
recurseDir = dir:
|
2023-07-26 08:43:14 +02:00
|
|
|
lib.mapAttrs
|
2023-02-14 10:10:08 +01:00
|
|
|
(file: type:
|
|
|
|
if type == "directory"
|
|
|
|
then recurseDir "${dir}/${file}"
|
2023-09-06 19:53:03 +02:00
|
|
|
else type)
|
2023-02-14 10:10:08 +01:00
|
|
|
(builtins.readDir dir);
|
|
|
|
|
2023-07-25 09:32:30 +02:00
|
|
|
allSatisfy = predicate: attrs: attrset:
|
2023-09-06 19:53:03 +02:00
|
|
|
lib.all
|
|
|
|
(attr:
|
|
|
|
and [
|
|
|
|
(builtins.hasAttr attr attrset)
|
|
|
|
(predicate (builtins.getAttr attr attrset))
|
|
|
|
])
|
|
|
|
attrs;
|
2023-02-14 10:10:08 +01:00
|
|
|
|
2023-07-25 09:32:30 +02:00
|
|
|
# NOTE: Implying last argument is the output of `recurseDir`
|
|
|
|
hasFiles = allSatisfy (eq "regular");
|
|
|
|
|
|
|
|
# NOTE: Implying last argument is the output of `recurseDir`
|
|
|
|
hasDirectories = allSatisfy lib.isAttrs;
|
2023-04-23 01:39:34 +02:00
|
|
|
|
2023-02-14 10:10:08 +01:00
|
|
|
# pkgs helpers
|
|
|
|
forEachSystem = lib.genAttrs [
|
|
|
|
"aarch64-linux"
|
|
|
|
"i686-linux"
|
|
|
|
"x86_64-linux"
|
|
|
|
"aarch64-darwin"
|
|
|
|
"x86_64-darwin"
|
|
|
|
];
|
|
|
|
|
|
|
|
forEachPkgs = f:
|
|
|
|
forEachSystem
|
|
|
|
(system:
|
|
|
|
f nixpkgs.legacyPackages.${system});
|
|
|
|
|
2023-07-25 09:32:30 +02:00
|
|
|
# Modules helpers
|
|
|
|
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
|
2023-09-06 19:53:03 +02:00
|
|
|
moduleDir = lib.path.append baseDir "${name}";
|
2023-07-25 09:32:30 +02:00
|
|
|
in
|
|
|
|
if and [
|
|
|
|
(type == "directory")
|
2023-07-29 17:12:22 +02:00
|
|
|
(hasFiles [ "default.nix" ] (builtins.readDir moduleDir))
|
2023-07-25 09:32:30 +02:00
|
|
|
] 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
|
2023-07-29 17:12:22 +02:00
|
|
|
(lib.pipe module [ builtins.functionArgs builtins.attrNames ])
|
|
|
|
(lib.pipe passthru [ builtins.attrNames ]))
|
2023-07-25 09:32:30 +02:00
|
|
|
]
|
|
|
|
then module passthru
|
|
|
|
else module))
|
|
|
|
];
|
|
|
|
|
2023-02-14 10:10:08 +01:00
|
|
|
# Modules
|
2023-07-29 23:00:17 +02:00
|
|
|
nixosModules = createModules ../modules/nixos { };
|
|
|
|
nixOnDroidModules = createModules ../modules/nix-on-droid { };
|
|
|
|
nixDarwinModules = createModules ../modules/nix-darwin { };
|
2023-07-25 09:32:30 +02:00
|
|
|
homeManagerModules = createModules ../modules/home-manager { };
|
2023-02-14 10:10:08 +01:00
|
|
|
|
|
|
|
# Machines
|
2023-07-29 23:00:17 +02:00
|
|
|
machines = recurseDir ../machines;
|
2023-02-14 10:10:08 +01:00
|
|
|
homeManagerMachines = machines.home-manager or { };
|
2023-07-29 23:00:17 +02:00
|
|
|
nixDarwinMachines = machines.nix-darwin or { };
|
|
|
|
nixOnDroidMachines = machines.nix-on-droid or { };
|
|
|
|
nixosMachines = machines.nixos or { };
|
2023-02-14 10:10:08 +01:00
|
|
|
|
|
|
|
# Configuration helpers
|
|
|
|
mkNixosHost = root: system: hostname: users: lib.nixosSystem {
|
|
|
|
inherit system;
|
|
|
|
|
|
|
|
modules = [
|
2023-09-06 19:53:03 +02:00
|
|
|
(lib.path.append root "configuration.nix")
|
2023-07-25 09:32:30 +02:00
|
|
|
inputs.home-manager.nixosModules.home-manager
|
2023-12-25 17:49:26 +01:00
|
|
|
{
|
|
|
|
nixpkgs.overlays = builtins.attrValues outputs.overlays;
|
|
|
|
}
|
2023-02-14 10:10:08 +01:00
|
|
|
{
|
|
|
|
home-manager = {
|
|
|
|
useGlobalPkgs = false;
|
|
|
|
useUserPackages = true;
|
|
|
|
users = lib.attrsets.genAttrs
|
|
|
|
users
|
2023-09-06 19:53:03 +02:00
|
|
|
(user: import (lib.path.append root "home/${user}.nix"));
|
2023-02-17 01:42:11 +01:00
|
|
|
sharedModules = builtins.attrValues homeManagerModules;
|
2023-02-14 10:10:08 +01:00
|
|
|
extraSpecialArgs = {
|
|
|
|
inherit inputs outputs;
|
2023-07-26 08:43:14 +02:00
|
|
|
inherit hostname;
|
2023-02-14 10:10:08 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
2023-07-25 09:32:30 +02:00
|
|
|
{
|
2023-07-29 23:00:17 +02:00
|
|
|
networking.hostName = lib.mkDefault hostname;
|
2023-07-25 09:32:30 +02:00
|
|
|
}
|
2023-02-14 10:10:08 +01:00
|
|
|
] ++ (builtins.attrValues nixosModules);
|
|
|
|
|
|
|
|
specialArgs = {
|
|
|
|
inherit inputs outputs;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
mkNixOnDroidHost = root: system: hostname: inputs.nix-on-droid.lib.nixOnDroidConfiguration {
|
|
|
|
pkgs = import nixpkgs {
|
|
|
|
inherit system;
|
|
|
|
|
|
|
|
overlays = [
|
|
|
|
inputs.nix-on-droid.overlays.default
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
modules = [
|
2023-09-06 19:53:03 +02:00
|
|
|
(lib.path.append root "configuration.nix")
|
2023-02-14 10:10:08 +01:00
|
|
|
{ nix.registry.nixpkgs.flake = nixpkgs; }
|
2023-12-25 17:49:26 +01:00
|
|
|
{
|
|
|
|
nixpkgs.overlays = builtins.attrValues outputs.overlays;
|
|
|
|
}
|
2023-02-17 02:21:03 +01:00
|
|
|
{
|
|
|
|
home-manager = {
|
2023-09-06 19:53:03 +02:00
|
|
|
config = (lib.path.append root "home.nix");
|
2023-02-17 02:21:03 +01:00
|
|
|
backupFileExtension = "hm-bak";
|
|
|
|
useGlobalPkgs = false;
|
|
|
|
useUserPackages = true;
|
|
|
|
sharedModules = builtins.attrValues homeManagerModules;
|
2023-07-26 08:43:14 +02:00
|
|
|
extraSpecialArgs = {
|
|
|
|
inherit inputs outputs;
|
|
|
|
inherit hostname;
|
|
|
|
};
|
2023-02-17 02:21:03 +01:00
|
|
|
};
|
|
|
|
}
|
2023-02-14 10:10:08 +01:00
|
|
|
] ++ (builtins.attrValues nixOnDroidModules);
|
|
|
|
|
|
|
|
extraSpecialArgs = {
|
|
|
|
inherit inputs outputs;
|
2023-07-26 08:43:14 +02:00
|
|
|
inherit hostname;
|
2023-02-14 10:10:08 +01:00
|
|
|
# rootPath = ./.;
|
|
|
|
};
|
|
|
|
|
|
|
|
home-manager-path = inputs.home-manager.outPath;
|
|
|
|
};
|
|
|
|
|
|
|
|
mkNixDarwinHost = root: system: hostname: users: inputs.nix-darwin.lib.darwinSystem {
|
|
|
|
inherit system;
|
|
|
|
|
|
|
|
modules = [
|
2023-09-06 19:53:03 +02:00
|
|
|
(lib.path.append root "configuration.nix")
|
2023-12-25 17:49:26 +01:00
|
|
|
{
|
|
|
|
nixpkgs.hostPlatform = system;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
nixpkgs.overlays = builtins.attrValues outputs.overlays;
|
|
|
|
}
|
2023-02-14 10:10:08 +01:00
|
|
|
inputs.home-manager.darwinModules.home-manager
|
|
|
|
{
|
|
|
|
home-manager = {
|
|
|
|
useGlobalPkgs = false;
|
|
|
|
useUserPackages = true;
|
|
|
|
users = lib.attrsets.genAttrs
|
|
|
|
users
|
2023-09-06 19:53:03 +02:00
|
|
|
(user: import (lib.path.append root "home/${user}.nix"));
|
2023-04-17 10:44:54 +02:00
|
|
|
sharedModules = builtins.attrValues homeManagerModules;
|
2023-07-26 08:43:14 +02:00
|
|
|
extraSpecialArgs = {
|
|
|
|
inherit inputs outputs;
|
|
|
|
inherit hostname;
|
|
|
|
};
|
2023-02-14 10:10:08 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
] ++ (builtins.attrValues nixDarwinModules);
|
|
|
|
|
2023-12-25 17:49:26 +01:00
|
|
|
specialArgs = {
|
2023-07-26 08:43:14 +02:00
|
|
|
inherit inputs outputs;
|
|
|
|
};
|
2023-02-14 10:10:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
mkHomeManagerHost = root: system: hostname: inputs.home-manager.lib.homeManagerConfiguration {
|
|
|
|
pkgs = nixpkgs.legacyPackages.${system};
|
|
|
|
|
|
|
|
modules = [
|
2023-09-06 19:53:03 +02:00
|
|
|
(lib.path.append root "home.nix")
|
2023-12-25 17:49:26 +01:00
|
|
|
{
|
|
|
|
nixpkgs.overlays = builtins.attrValues outputs.overlays;
|
|
|
|
}
|
2023-02-14 10:10:08 +01:00
|
|
|
] ++ (builtins.attrValues homeManagerModules);
|
|
|
|
|
2023-07-26 08:43:14 +02:00
|
|
|
extraSpecialArgs = {
|
|
|
|
inherit inputs outputs;
|
|
|
|
inherit hostname;
|
|
|
|
};
|
2023-02-14 10:10:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
createConfigurations =
|
|
|
|
pred: mkHost: machines:
|
|
|
|
lib.foldAttrs
|
2023-04-23 01:39:34 +02:00
|
|
|
lib.const
|
2023-02-14 10:10:08 +01:00
|
|
|
[ ]
|
|
|
|
(builtins.attrValues
|
|
|
|
(builtins.mapAttrs
|
|
|
|
(system: hosts:
|
2023-09-07 00:17:53 +02:00
|
|
|
lib.concatMapAttrs
|
2023-02-14 10:10:08 +01:00
|
|
|
(host: config:
|
2023-09-07 00:17:53 +02:00
|
|
|
lib.optionalAttrs
|
|
|
|
(and [
|
|
|
|
(host != "__template__")
|
|
|
|
(pred system host config)
|
|
|
|
])
|
|
|
|
{
|
|
|
|
${host} = mkHost system host config;
|
|
|
|
})
|
|
|
|
hosts)
|
2023-02-14 10:10:08 +01:00
|
|
|
machines));
|
|
|
|
|
|
|
|
# Configurations
|
|
|
|
autoNixosConfigurations =
|
|
|
|
createConfigurations
|
|
|
|
(system: host: config:
|
2023-04-23 01:39:34 +02:00
|
|
|
and
|
|
|
|
[
|
|
|
|
(hasFiles
|
|
|
|
[ "configuration.nix" ]
|
|
|
|
config)
|
2023-09-06 19:53:03 +02:00
|
|
|
# (hasDirectories
|
|
|
|
# [ "home" ]
|
|
|
|
# config)
|
2023-04-23 01:39:34 +02:00
|
|
|
])
|
2023-02-14 10:10:08 +01:00
|
|
|
(system: host: config:
|
|
|
|
mkNixosHost
|
2023-02-17 01:42:11 +01:00
|
|
|
../machines/nixos/${system}/${host}
|
2023-02-14 10:10:08 +01:00
|
|
|
system
|
|
|
|
host
|
|
|
|
(builtins.map
|
|
|
|
(lib.strings.removeSuffix ".nix")
|
|
|
|
(builtins.attrNames (config."home" or { }))))
|
|
|
|
nixosMachines;
|
|
|
|
|
|
|
|
autoNixOnDroidConfigurations =
|
|
|
|
createConfigurations
|
|
|
|
(system: host: config:
|
2023-04-23 01:39:34 +02:00
|
|
|
and
|
|
|
|
[
|
|
|
|
(hasFiles
|
|
|
|
[ "configuration.nix" "home.nix" ]
|
|
|
|
config)
|
|
|
|
])
|
2023-02-14 10:10:08 +01:00
|
|
|
(system: host: config:
|
|
|
|
mkNixOnDroidHost
|
2023-02-17 01:42:11 +01:00
|
|
|
../machines/nix-on-droid/${system}/${host}
|
2023-02-14 10:10:08 +01:00
|
|
|
system
|
|
|
|
host)
|
|
|
|
nixOnDroidMachines;
|
|
|
|
|
|
|
|
autoDarwinConfigurations =
|
|
|
|
createConfigurations
|
|
|
|
(system: host: config:
|
2023-04-23 01:39:34 +02:00
|
|
|
and
|
|
|
|
[
|
|
|
|
(hasFiles
|
|
|
|
[ "configuration.nix" ]
|
|
|
|
config)
|
|
|
|
(hasDirectories
|
|
|
|
[ "home" ]
|
|
|
|
config)
|
|
|
|
])
|
2023-02-14 10:10:08 +01:00
|
|
|
(system: host: config:
|
|
|
|
mkNixDarwinHost
|
2023-02-17 01:42:11 +01:00
|
|
|
../machines/nix-darwin/${system}/${host}
|
2023-02-14 10:10:08 +01:00
|
|
|
system
|
|
|
|
host
|
|
|
|
(builtins.map
|
|
|
|
(lib.strings.removeSuffix ".nix")
|
|
|
|
(builtins.attrNames (config."home" or { }))))
|
|
|
|
nixDarwinMachines;
|
|
|
|
|
|
|
|
autoHomeConfigurations =
|
|
|
|
createConfigurations
|
|
|
|
(system: host: config:
|
2023-04-23 01:39:34 +02:00
|
|
|
and
|
|
|
|
[
|
|
|
|
(hasFiles
|
|
|
|
[ "home.nix" ]
|
|
|
|
config)
|
|
|
|
])
|
2023-02-14 10:10:08 +01:00
|
|
|
(system: host: config:
|
|
|
|
mkHomeManagerHost
|
2023-02-17 01:42:11 +01:00
|
|
|
../machines/home-manager/${system}/${host}
|
2023-02-14 10:10:08 +01:00
|
|
|
system
|
|
|
|
host)
|
|
|
|
homeManagerMachines;
|
2023-07-29 17:12:22 +02:00
|
|
|
|
2023-07-29 23:00:17 +02:00
|
|
|
# Automatic deploy.rs nodes (for NixOS and nix-darwin)
|
|
|
|
|
|
|
|
gen-config-type-to = mappings: mkError: config-type:
|
|
|
|
mappings.${config-type} or
|
|
|
|
(builtins.throw
|
|
|
|
(mkError config-type));
|
|
|
|
|
|
|
|
config-type-to-outputs-machines =
|
|
|
|
gen-config-type-to
|
|
|
|
{
|
|
|
|
nixos = "nixosMachines";
|
|
|
|
nix-on-droid = "nixOnDroidMachines";
|
|
|
|
nix-darwin = "nixDarwinMachines";
|
|
|
|
home-manager = "homeMachines";
|
|
|
|
}
|
|
|
|
(config-type:
|
|
|
|
builtins.throw
|
|
|
|
"Invaild config-type \"${config-type}\" for flake outputs' machines");
|
|
|
|
|
|
|
|
config-type-to-outputs-configurations =
|
|
|
|
gen-config-type-to
|
|
|
|
{
|
|
|
|
nixos = "nixosConfigurations";
|
|
|
|
nix-on-droid = "nixOnDroidConfigurations";
|
|
|
|
nix-darwin = "darwinConfigurations";
|
|
|
|
home-manager = "homeConfigurations";
|
|
|
|
}
|
|
|
|
(config-type:
|
|
|
|
builtins.throw
|
|
|
|
"Invaild config-type \"${config-type}\" for flake outputs' configurations");
|
|
|
|
|
|
|
|
config-type-to-deploy-type =
|
|
|
|
gen-config-type-to
|
|
|
|
{
|
|
|
|
nixos = "nixos";
|
|
|
|
nix-darwin = "darwin";
|
|
|
|
}
|
|
|
|
(config-type:
|
|
|
|
builtins.throw
|
|
|
|
"Invaild config-type \"${config-type}\" for deploy-rs deployment");
|
|
|
|
|
2023-07-29 17:12:22 +02:00
|
|
|
deploy.autoNodes =
|
2023-07-29 23:00:17 +02:00
|
|
|
lib.flip lib.concatMapAttrs
|
|
|
|
(lib.genAttrs
|
|
|
|
[
|
|
|
|
"nixos"
|
|
|
|
"nix-darwin"
|
|
|
|
]
|
|
|
|
(config-type:
|
|
|
|
let
|
|
|
|
machines = config-type-to-outputs-machines config-type;
|
|
|
|
in
|
|
|
|
outputs.${machines}))
|
|
|
|
(config-type: machines:
|
|
|
|
lib.pipe
|
|
|
|
machines
|
|
|
|
[
|
|
|
|
# Filter out nondirectories
|
|
|
|
(lib.filterAttrs
|
|
|
|
(system: configs:
|
|
|
|
builtins.isAttrs configs))
|
|
|
|
# Convert non-template configs into `system-and-config` pairs
|
|
|
|
(lib.concatMapAttrs
|
|
|
|
(system: configs:
|
|
|
|
(lib.concatMapAttrs
|
|
|
|
(host: config:
|
|
|
|
lib.optionalAttrs
|
|
|
|
(host != "__template__")
|
|
|
|
{
|
|
|
|
${host} = {
|
|
|
|
inherit system;
|
|
|
|
config =
|
|
|
|
let
|
|
|
|
configurations = config-type-to-outputs-configurations config-type;
|
|
|
|
in
|
|
|
|
outputs.${configurations}.${host};
|
|
|
|
};
|
|
|
|
})
|
|
|
|
configs)))
|
|
|
|
# Convert each `system-and-config` pair into a deploy-rs node
|
|
|
|
(lib.concatMapAttrs
|
|
|
|
(host: { system, config }:
|
|
|
|
let
|
|
|
|
deploy-config-path =
|
|
|
|
../machines/${config-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 = config-type-to-deploy-type config-type;
|
|
|
|
in
|
|
|
|
inputs.deploy-rs.lib.${system}.activate.${deploy-type} config;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}))
|
|
|
|
]);
|
2023-07-29 17:12:22 +02:00
|
|
|
|
2023-07-29 23:00:17 +02:00
|
|
|
autoChecks =
|
|
|
|
lib.mapAttrs
|
|
|
|
(system: deployLib:
|
|
|
|
deployLib.deployChecks
|
|
|
|
outputs.deploy)
|
|
|
|
inputs.deploy-rs.lib;
|
2023-02-14 10:10:08 +01:00
|
|
|
}
|