Modularize flake utilities

This commit is contained in:
Gabriel Arazas 2021-12-06 17:55:40 +08:00
parent 889742c402
commit d4f15e4c0b
3 changed files with 92 additions and 7 deletions

View File

@ -93,7 +93,7 @@
# A list of NixOS configurations from the `./hosts` folder.
# It also has some sensible default configurations.
nixosConfigurations = libExtended.mapAttrs
nixosConfigurations = libExtended.mapAttrsRecursive
(host: path: libExtended.flakeUtils.mkHost path hostDefaultConfig)
(libExtended.filesToAttr ./hosts);
@ -102,12 +102,15 @@
nixosModules = libExtended.mapAttrsRecursive (_: path: import path)
(libExtended.filesToAttr ./modules/nixos);
# This will make importing user-specific configurations even easier on non-NixOS systems!
# I can now install home-manager users in non-NixOS systems.
# NICE!
homeManagerConfigurations = libExtended.mapAttrs (user: path: libExtended.flakeUtils.mkUser path userDefaultConfig) (libExtended.filesToAttr ./users/home-manager);
homeManagerConfigurations = libExtended.mapAttrs
(_: path: libExtended.flakeUtils.mkUser path userDefaultConfig)
(libExtended.filesToAttr ./users/home-manager);
# In case anybody want my modules for whatever reason, here you go.
homeManagerModules = libExtended.mapAttrsRecursive (_: path: import path) (libExtended.filesToAttr ./modules/home-manager);
# Extending home-manager with my custom modules, if anyone cares.
homeManagerModules = libExtended.mapAttrsRecursive (_: path: import path)
(libExtended.filesToAttr ./modules/home-manager);
# My custom packages, available in here as well.
# Though, I mainly support "x86_64-linux".

View File

@ -1,9 +1,10 @@
{ lib, inputs, ... }:
let
# Default system for our host configuration.
sys = "x86_64-linux";
flakeUtils = import ./flake-utils.nix { inherit lib inputs; };
in rec {
inherit flakeUtils;
/* Create an attribute set that represents the structure of the modules
inside of a directory. While it can recurse into directories, it will
stop once it detects `default.nix` inside.

81
lib/flake-utils.nix Normal file
View File

@ -0,0 +1,81 @@
# A list of utilities specifically in my flake output.
{ lib, inputs, ... }:
let
# Default system for our host configuration.
sys = "x86_64-linux";
in rec {
/* Create a NixOS system through a given host folder.
It will automate some of the things such as making the last component
of the path as the hostname.
This is a wrapper for `nixpkgs.lib.nixosSystem`.
Signature:
path -> attrset -> NixOS configuration
Where:
- `path` is a path to a Nix file for the host; the basename of the file
is also used as the hostname
- `attrset` is the attribute set to be included in the host configuration
Returns:
An attribute set from the `lib.nixosSystem` from `nixpkgs` flake.
Example:
mkHost ./hosts/june {}
=> { ... } # NixOS configuration attrset
*/
mkHost = file:
attrs@{ system ? sys, ... }:
lib.nixosSystem {
inherit system;
# Additional attributes to be referred to our modules.
specialArgs = { inherit lib system inputs; };
# We also set the following in order for priority.
# Later modules will override previously imported modules.
modules = [
# Set the hostname.
{
networking.hostName = builtins.baseNameOf file;
}
# Put the given attribute set (except for the system).
(lib.filterAttrs (n: v: !lib.elem n [ "system" ]) attrs)
# The entry point of the module.
file
]
# Append with our custom NixOS modules from the modules folder.
++ (lib.modulesToList (lib.filesToAttr ../modules/nixos));
};
/* Create a home-manager configuration for use in flakes.
This is a wrapper for `home-manager.lib.homeManagerConfiguration`.
Signature:
file -> attrset -> homeManagerConfiguration
Where:
- `file` is the entry point to the home-manager configuration.
- `attrset` is the additional attribute set to be insert as one of the imported modules minus the attributes used for `home-manager.lib.homeManagerConfiguration`.
Returns:
A home-manager configuration to be exported in flakes.
Example:
mkUser ./users/foo-dogsquared {}
=> { ... } # A home-manager configuration set.
*/
mkUser = file: attrs@{ username ? (builtins.baseNameOf file), system ? sys, extraModules ? [], ... }:
let
hmConfigFunctionArgs = builtins.attrNames (builtins.functionArgs inputs.home-manager.lib.homeManagerConfiguration);
hmModules = lib.map (path: import path) (lib.modulesToList (lib.filesToAttrRec ../modules/home-manager));
in
inputs.home-manager.lib.homeManagerConfiguration {
inherit system username;
configuration = import file;
homeDirectory = "/home/${username}";
extraModules = hmModules ++ extraModules ++ [ (lib.filterAttrs (n: _: !lib.elem n hmConfigFunctionArgs) attrs) ];
extraSpecialArgs = { inherit system; };
};
}