feat!(flake): convert to fs-defined configurations

Place all configurations in `machines/${manager}/${system}/${hostname}/...`:
    - `${manager}` - One of `nixos`, `nix-on-droid`, `nix-darwin` or `home-manager`
    - `${system}` - A system's architecture (can be many)
    - `${hostname}` - A system's hostname (can be many)

The flake now automatically generates the needed configurations based on the above structure.

It only generates configurations for valid directory structures:
    - It wouldn't generate a NixOS config if there isn't a `configuration.nix` file
    - ...
This commit is contained in:
reo101 2022-11-27 02:12:21 +02:00
parent b0110b3e64
commit dd2391d905
9 changed files with 176 additions and 66 deletions

138
flake.nix
View file

@ -67,6 +67,10 @@
} @ inputs:
let
inherit (self) outputs;
inherit ((import ./lib/default.nix) { lib = nixpkgs.lib; })
recurseDir
hasFiles
hasDirectories;
forAllSystems = nixpkgs.lib.genAttrs [
"aarch64-linux"
"i686-linux"
@ -87,22 +91,32 @@
);
overlays = import ./overlays;
# Modules
nixosModules = import ./modules/nixos;
nixOnDroidModules = import ./modules/nix-on-droid;
nixDarwinModules = import ./modules/nix-darwin;
homeManagerModules = import ./modules/home-manager;
nixosConfigurations = {
# arthur = nixpkgs.lib.nixosSystem {
# specialArgs = { inherit inputs outputs; };
# modules = [
# ./nixos/arthur/configuration.nix
# ];
# };
# Machines
machines = recurseDir ./machines;
homeManagerMachines = machines.home-manager or {};
nixDarwinMachines = machines.nix-darwin or {};
nixOnDroidMachines = machines.nix-on-droid or {};
nixosMachines = machines.nixos or {};
# mkHost helpers
mkNixosHost = system: hostname: nixpkgs.lib.nixosSystem {
modules = [
./machines/nixos/${system}/${hostname}/configuration.nix
];
specialArgs = {
inherit inputs outputs;
};
};
nixOnDroidConfigurations =
let mkHost = system: hostname: nix-on-droid.lib.nixOnDroidConfiguration {
mkNixOnDroidHost = system: hostname: nix-on-droid.lib.nixOnDroidConfiguration {
pkgs = import nixpkgs {
inherit system;
@ -112,7 +126,7 @@
};
modules = [
./nix-on-droid/${hostname}/configuration.nix
./machines/nix-on-droid/${system}/${hostname}/configuration.nix
{ nix.registry.nixpkgs.flake = nixpkgs; }
] ++ (builtins.attrValues nixOnDroidModules);
@ -123,18 +137,11 @@
home-manager-path = home-manager.outPath;
};
in
rec {
cheetah = mkHost "aarch64-linux" "cheetah";
default = cheetah;
};
darwinConfigurations =
let mkHost = system: hostname: users: nix-darwin.lib.darwinSystem {
mkNixDarwinHost = system: hostname: users: nix-darwin.lib.darwinSystem {
inherit system;
modules = [
./nix-darwin/${hostname}/configuration.nix
./machines/nix-darwin/${system}/${hostname}/configuration.nix
home-manager.darwinModules.home-manager
{
home-manager = {
@ -142,7 +149,7 @@
useUserPackages = true;
users = nixpkgs.lib.attrsets.genAttrs
users
(user: import ./nix-darwin/${hostname}/home/${user}.nix);
(user: import ./machines/nix-darwin/${system}/${hostname}/home/${user}.nix);
extraSpecialArgs = { inherit inputs; };
};
@ -150,19 +157,86 @@
] ++ (builtins.attrValues nixDarwinModules);
inputs = { inherit inputs outputs nix-darwin nixpkgs; };
};
in
rec {
apavel-a01 = mkHost "x86_64-darwin" "apavel-a01" [ "apavel" ];
mkHomeManagerHost = system: hostname: home-manager.lib.homeManagerConfiguration {
pkgs = nixpkgs.legacyPackages.${system};
modules = [
./machines/home-manager/${system}/${hostname}/home.nix
];
extraSpecialArgs = { inherit inputs outputs; };
};
homeConfigurations = {
# "nix-on-droid@cheetah" = home-manager.lib.homeManagerConfiguration {
# pkgs = nixpkgs.legacyPackages.x86_64-linux;
# extraSpecialArgs = { inherit inputs outputs; };
# modules = [
# ./home-manager/home.nix
# ];
# };
};
# Final configurations
nixosConfigurations =
nixpkgs.lib.foldAttrs
(acc: x: acc)
[]
(builtins.attrValues
(builtins.mapAttrs
(system: hosts:
nixpkgs.lib.attrsets.filterAttrs
(host: config: config != null)
(builtins.mapAttrs
(host: config:
if (hasFiles [ "configuration.nix" ] config)
then mkNixosHost system host
else null)
hosts))
nixosMachines));
nixOnDroidConfigurations =
nixpkgs.lib.foldAttrs
(acc: x: acc)
[]
(builtins.attrValues
(builtins.mapAttrs
(system: hosts:
nixpkgs.lib.attrsets.filterAttrs
(host: config: config != null)
(builtins.mapAttrs
(host: config:
if (hasFiles [ "configuration.nix" "home.nix" ] config)
then mkNixOnDroidHost system host
else null)
hosts))
nixOnDroidMachines));
darwinConfigurations =
nixpkgs.lib.foldAttrs
(acc: x: acc)
[]
(builtins.attrValues
(builtins.mapAttrs
(system: hosts:
nixpkgs.lib.attrsets.filterAttrs
(host: config: config != null)
(builtins.mapAttrs
(host: config:
if (hasFiles [ "configuration.nix" ] config)
then mkNixDarwinHost system host
(builtins.map
(nixpkgs.lib.strings.removeSuffix ".nix")
(builtins.attrNames (config."home" or {})))
else null)
hosts))
nixDarwinMachines));
homeConfigurations =
nixpkgs.lib.foldAttrs
(acc: x: acc)
[]
(builtins.attrValues
(builtins.mapAttrs
(system: hosts:
nixpkgs.lib.attrsets.filterAttrs
(host: config: config != null)
(builtins.mapAttrs
(host: config:
if (hasFiles [ "home.nix" ] config)
then mkNixOnDroidHost system host
else null)
hosts))
homeManagerMachines));
};
}

36
lib/default.nix Normal file
View file

@ -0,0 +1,36 @@
{ lib, ... }:
let
inherit (lib) mapAttrs;
inherit (lib.attrsets) filterAttrs;
in
rec {
recurseDir = dir:
mapAttrs
(file: type:
if type == "directory"
then recurseDir "${dir}/${file}"
else type
)
(builtins.readDir dir);
# VVV - Implying `attrs` is the output of `recurseDir` - VVV
hasFiles = files: attrs:
builtins.all
(b: b)
(builtins.map
(file:
builtins.hasAttr file attrs &&
builtins.getAttr file attrs == "regular")
files);
hasDirectories = directories: attrs:
builtins.all
(b: b)
(builtins.map
(directory:
builtins.hasAttr directory attrs &&
builtins.getAttr directory attrs == "set")
directories);
}