mirror of
https://github.com/foo-dogsquared/nixos-config.git
synced 2025-02-12 06:19:00 +00:00
Modularize flake utilities
This commit is contained in:
parent
889742c402
commit
d4f15e4c0b
13
flake.nix
13
flake.nix
@ -93,7 +93,7 @@
|
|||||||
|
|
||||||
# A list of NixOS configurations from the `./hosts` folder.
|
# A list of NixOS configurations from the `./hosts` folder.
|
||||||
# It also has some sensible default configurations.
|
# It also has some sensible default configurations.
|
||||||
nixosConfigurations = libExtended.mapAttrs
|
nixosConfigurations = libExtended.mapAttrsRecursive
|
||||||
(host: path: libExtended.flakeUtils.mkHost path hostDefaultConfig)
|
(host: path: libExtended.flakeUtils.mkHost path hostDefaultConfig)
|
||||||
(libExtended.filesToAttr ./hosts);
|
(libExtended.filesToAttr ./hosts);
|
||||||
|
|
||||||
@ -102,12 +102,15 @@
|
|||||||
nixosModules = libExtended.mapAttrsRecursive (_: path: import path)
|
nixosModules = libExtended.mapAttrsRecursive (_: path: import path)
|
||||||
(libExtended.filesToAttr ./modules/nixos);
|
(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!
|
# 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.
|
# Extending home-manager with my custom modules, if anyone cares.
|
||||||
homeManagerModules = libExtended.mapAttrsRecursive (_: path: import path) (libExtended.filesToAttr ./modules/home-manager);
|
homeManagerModules = libExtended.mapAttrsRecursive (_: path: import path)
|
||||||
|
(libExtended.filesToAttr ./modules/home-manager);
|
||||||
|
|
||||||
# My custom packages, available in here as well.
|
# My custom packages, available in here as well.
|
||||||
# Though, I mainly support "x86_64-linux".
|
# Though, I mainly support "x86_64-linux".
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
{ lib, inputs, ... }:
|
{ lib, inputs, ... }:
|
||||||
|
|
||||||
let
|
let
|
||||||
# Default system for our host configuration.
|
flakeUtils = import ./flake-utils.nix { inherit lib inputs; };
|
||||||
sys = "x86_64-linux";
|
|
||||||
in rec {
|
in rec {
|
||||||
|
inherit flakeUtils;
|
||||||
|
|
||||||
/* Create an attribute set that represents the structure of the modules
|
/* Create an attribute set that represents the structure of the modules
|
||||||
inside of a directory. While it can recurse into directories, it will
|
inside of a directory. While it can recurse into directories, it will
|
||||||
stop once it detects `default.nix` inside.
|
stop once it detects `default.nix` inside.
|
||||||
|
81
lib/flake-utils.nix
Normal file
81
lib/flake-utils.nix
Normal 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; };
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user