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

206
flake.nix
View file

@ -67,6 +67,10 @@
} @ inputs: } @ inputs:
let let
inherit (self) outputs; inherit (self) outputs;
inherit ((import ./lib/default.nix) { lib = nixpkgs.lib; })
recurseDir
hasFiles
hasDirectories;
forAllSystems = nixpkgs.lib.genAttrs [ forAllSystems = nixpkgs.lib.genAttrs [
"aarch64-linux" "aarch64-linux"
"i686-linux" "i686-linux"
@ -87,82 +91,152 @@
); );
overlays = import ./overlays; overlays = import ./overlays;
# Modules
nixosModules = import ./modules/nixos; nixosModules = import ./modules/nixos;
nixOnDroidModules = import ./modules/nix-on-droid; nixOnDroidModules = import ./modules/nix-on-droid;
nixDarwinModules = import ./modules/nix-darwin; nixDarwinModules = import ./modules/nix-darwin;
homeManagerModules = import ./modules/home-manager; homeManagerModules = import ./modules/home-manager;
nixosConfigurations = { # Machines
# arthur = nixpkgs.lib.nixosSystem { machines = recurseDir ./machines;
# specialArgs = { inherit inputs outputs; }; homeManagerMachines = machines.home-manager or {};
# modules = [ nixDarwinMachines = machines.nix-darwin or {};
# ./nixos/arthur/configuration.nix 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;
};
}; };
mkNixOnDroidHost = system: hostname: nix-on-droid.lib.nixOnDroidConfiguration {
pkgs = import nixpkgs {
inherit system;
overlays = [
nix-on-droid.overlays.default
];
};
modules = [
./machines/nix-on-droid/${system}/${hostname}/configuration.nix
{ nix.registry.nixpkgs.flake = nixpkgs; }
] ++ (builtins.attrValues nixOnDroidModules);
extraSpecialArgs = {
inherit inputs outputs;
# rootPath = ./.;
};
home-manager-path = home-manager.outPath;
};
mkNixDarwinHost = system: hostname: users: nix-darwin.lib.darwinSystem {
inherit system;
modules = [
./machines/nix-darwin/${system}/${hostname}/configuration.nix
home-manager.darwinModules.home-manager
{
home-manager = {
useGlobalPkgs = false;
useUserPackages = true;
users = nixpkgs.lib.attrsets.genAttrs
users
(user: import ./machines/nix-darwin/${system}/${hostname}/home/${user}.nix);
extraSpecialArgs = { inherit inputs; };
};
}
] ++ (builtins.attrValues nixDarwinModules);
inputs = { inherit inputs outputs nix-darwin nixpkgs; };
};
mkHomeManagerHost = system: hostname: home-manager.lib.homeManagerConfiguration {
pkgs = nixpkgs.legacyPackages.${system};
modules = [
./machines/home-manager/${system}/${hostname}/home.nix
];
extraSpecialArgs = { inherit inputs outputs; };
};
# 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 = nixOnDroidConfigurations =
let mkHost = system: hostname: nix-on-droid.lib.nixOnDroidConfiguration { nixpkgs.lib.foldAttrs
pkgs = import nixpkgs { (acc: x: acc)
inherit system; []
(builtins.attrValues
overlays = [ (builtins.mapAttrs
nix-on-droid.overlays.default (system: hosts:
]; nixpkgs.lib.attrsets.filterAttrs
}; (host: config: config != null)
(builtins.mapAttrs
modules = [ (host: config:
./nix-on-droid/${hostname}/configuration.nix if (hasFiles [ "configuration.nix" "home.nix" ] config)
{ nix.registry.nixpkgs.flake = nixpkgs; } then mkNixOnDroidHost system host
] ++ (builtins.attrValues nixOnDroidModules); else null)
hosts))
extraSpecialArgs = { nixOnDroidMachines));
inherit inputs outputs;
# rootPath = ./.;
};
home-manager-path = home-manager.outPath;
};
in
rec {
cheetah = mkHost "aarch64-linux" "cheetah";
default = cheetah;
};
darwinConfigurations = darwinConfigurations =
let mkHost = system: hostname: users: nix-darwin.lib.darwinSystem { nixpkgs.lib.foldAttrs
inherit system; (acc: x: acc)
modules = [ []
./nix-darwin/${hostname}/configuration.nix (builtins.attrValues
home-manager.darwinModules.home-manager (builtins.mapAttrs
{ (system: hosts:
home-manager = { nixpkgs.lib.attrsets.filterAttrs
useGlobalPkgs = false; (host: config: config != null)
useUserPackages = true; (builtins.mapAttrs
users = nixpkgs.lib.attrsets.genAttrs (host: config:
users if (hasFiles [ "configuration.nix" ] config)
(user: import ./nix-darwin/${hostname}/home/${user}.nix); then mkNixDarwinHost system host
(builtins.map
(nixpkgs.lib.strings.removeSuffix ".nix")
(builtins.attrNames (config."home" or {})))
else null)
hosts))
nixDarwinMachines));
extraSpecialArgs = { inherit inputs; }; homeConfigurations =
}; nixpkgs.lib.foldAttrs
} (acc: x: acc)
] ++ (builtins.attrValues nixDarwinModules); []
inputs = { inherit inputs outputs nix-darwin nixpkgs; }; (builtins.attrValues
}; (builtins.mapAttrs
in (system: hosts:
rec { nixpkgs.lib.attrsets.filterAttrs
apavel-a01 = mkHost "x86_64-darwin" "apavel-a01" [ "apavel" ]; (host: config: config != null)
}; (builtins.mapAttrs
(host: config:
homeConfigurations = { if (hasFiles [ "home.nix" ] config)
# "nix-on-droid@cheetah" = home-manager.lib.homeManagerConfiguration { then mkNixOnDroidHost system host
# pkgs = nixpkgs.legacyPackages.x86_64-linux; else null)
# extraSpecialArgs = { inherit inputs outputs; }; hosts))
# modules = [ homeManagerMachines));
# ./home-manager/home.nix
# ];
# };
};
}; };
} }

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);
}