mjallen-lib Utility Functions
This directory contains utility functions that can be used to enhance your Nix configuration. These functions are inspired by the khanelinix repository and provide a more explicit and modular approach to building Nix configurations.
Directory Structure
default.nix: Main entry point that imports and exposes all utility functionsmodule/: Utilities for module creation and option handlingfile/: Utilities for file handling and module discoverysystem/: Utilities for system configuration building
How to Use
1. Import the Library
The library is already imported in your flake.nix file through the outputs-builder:
outputs-builder = channels: {
formatter = inputs.treefmt-nix.lib.mkWrapper channels.nixpkgs ./treefmt.nix;
# Add mjallen-lib to the flake outputs
overlays = {
mjallen-lib = final: prev: {
mjallen-lib = (import ./lib { inherit inputs; }).mjallen-lib;
};
};
};
This makes the mjallen-lib available to all your modules through the extended lib.
2. Use the Module Utilities
The module utilities provide functions for creating modules with consistent options:
{ lib, ... }:
let
inherit (lib.mjallen.module) mkModule mkOpt mkBoolOpt;
in
mkModule {
name = "mymodule";
description = "My awesome module";
options = {
setting1 = mkOpt lib.types.str "default" "Description of setting1";
setting2 = mkBoolOpt false "Description of setting2";
};
config = {
# Module implementation
};
}
3. Use the File Utilities
The file utilities provide functions for file handling and module discovery:
{ lib, ... }:
let
inherit (lib.mjallen.file) safeImport importModulesRecursive;
in
{
# Import a file with error handling
myConfig = safeImport ./my-config.nix {};
# Import all modules recursively
imports = importModulesRecursive ./modules;
}
4. Use the System Utilities
The system utilities provide functions for building system configurations:
{ lib, ... }:
let
inherit (lib.mjallen.system.common) mkHomeManagerConfig;
in
{
# Build home-manager configurations
homeManagerConfig = mkHomeManagerConfig {
extendedLib = lib;
inputs = inputs;
system = "x86_64-linux";
matchingHomes = { ... };
};
}
Available Functions
Module Utilities
mkModule: Create a module with common optionsmkOpt: Create an option with a type, default value, and descriptionmkOpt': Create an option with a type and default value (no description)mkBoolOpt: Create a boolean option with a default value and descriptionmkBoolOpt': Create a boolean option with a default value (no description)enabled: Standard enable patterndisabled: Standard disable patterncapitalize: Capitalize a stringboolToNum: Convert a boolean to a numberdefault-attrs: Apply mkDefault to all attributesforce-attrs: Apply mkForce to all attributesnested-default-attrs: Apply default-attrs to nested attributesnested-force-attrs: Apply force-attrs to nested attributes
File Utilities
readFile: Read a file and return its contentspathExists: Check if a file existssafeImport: Import a nix file with error handlingscanDir: Scan a directory and return directory namesgetFile: Get a file path relative to the flake rootimportModulesRecursive: Recursively discover and import all Nix modules in a directory treescanSystems: Recursively scan systems directory structurefilterNixOSSystems: Filter systems for NixOS (Linux)filterDarwinSystems: Filter systems for Darwin (macOS)scanHomes: Scan homes directory structure for home configurations
System Utilities
mkExtendedLib: Extend the nixpkgs lib with mjallen-libmkNixpkgsConfig: Create a nixpkgs configurationmkHomeConfigs: Create home configurations for a system and hostnamemkHomeManagerConfig: Create a home-manager configurationmkSpecialArgs: Create special arguments for a system configuration