From 6d00e0f544ddd7849a9104414c5d76cc15adb8a2 Mon Sep 17 00:00:00 2001 From: Archit Gupta Date: Mon, 23 Oct 2023 20:21:36 -0700 Subject: [PATCH] Allow functions for NixOS or home-manager configurations This allows a configuration to be set as a function that will be passed autoloadArgs. This is useful when each configuration is in its own file when autoloading. --- api_guide.md | 18 ++++++++++++++++++ builtinModules/homeConfigurations.nix | 12 +++++++----- builtinModules/nixosConfigurations.nix | 12 +++++++----- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/api_guide.md b/api_guide.md index 21df27d..783ead9 100644 --- a/api_guide.md +++ b/api_guide.md @@ -617,6 +617,10 @@ outputs for NixOS systems and home-manager users. They should be set to an attribute set of respective configurations. +Alternatively, the configurations can be functions, in which case those +functions will be passed `autoloadArgs` and must return a standard +configuration (this is useful when using autoloads with the `nixDir` feature). + For example: ```nix @@ -646,6 +650,20 @@ For example: } ``` +Optionally, defining as a function: + +```nix +{ + inputs.flakelight.url = "github:accelbread/flakelight"; + outputs = { flakelight, ... }: + flakelight ./. { + nixosConfigurations.system = { lib, ... }: lib.nixosSystem { + # nixosSystem arguments + }; + }; +} +``` + ### nixosModules, homeModules, and flakelightModules The `nixosModules`, `homeModules`, and `flakelightModules` options allow you to diff --git a/builtinModules/homeConfigurations.nix b/builtinModules/homeConfigurations.nix index 73b86da..2df9a01 100644 --- a/builtinModules/homeConfigurations.nix +++ b/builtinModules/homeConfigurations.nix @@ -2,12 +2,13 @@ # Copyright (C) 2023 Archit Gupta # SPDX-License-Identifier: MIT -{ config, lib, ... }: +{ config, lib, flakelight, autoloadArgs, ... }: let - inherit (builtins) isAttrs; + inherit (builtins) isAttrs mapAttrs; inherit (lib) foldl mapAttrsToList mergeOneOption mkOption mkOptionType mkIf recursiveUpdate; inherit (lib.types) lazyAttrsOf; + inherit (flakelight.types) optFunctionTo; homeConfiguration = mkOptionType { name = "homeConfiguration"; @@ -19,16 +20,17 @@ let in { options.homeConfigurations = mkOption { - type = lazyAttrsOf homeConfiguration; + type = lazyAttrsOf (optFunctionTo homeConfiguration); default = { }; }; config.outputs = mkIf (config.homeConfigurations != { }) { - inherit (config) homeConfigurations; + homeConfigurations = mapAttrs (_: f: f autoloadArgs) + config.homeConfigurations; checks = foldl recursiveUpdate { } (mapAttrsToList (n: v: { ${v.config.nixpkgs.system}."home-${n}" = v.activationPackage; }) - config.homeConfigurations); + (mapAttrs (_: f: f autoloadArgs) config.homeConfigurations)); }; } diff --git a/builtinModules/nixosConfigurations.nix b/builtinModules/nixosConfigurations.nix index 8fa1391..52a6a35 100644 --- a/builtinModules/nixosConfigurations.nix +++ b/builtinModules/nixosConfigurations.nix @@ -2,12 +2,13 @@ # Copyright (C) 2023 Archit Gupta # SPDX-License-Identifier: MIT -{ config, lib, ... }: +{ config, lib, flakelight, autoloadArgs, ... }: let - inherit (builtins) isAttrs; + inherit (builtins) isAttrs mapAttrs; inherit (lib) foldl mapAttrsToList mergeOneOption mkIf mkOption mkOptionType recursiveUpdate; inherit (lib.types) lazyAttrsOf; + inherit (flakelight.types) optFunctionTo; nixosConfiguration = mkOptionType { name = "nixosConfiguration"; @@ -21,17 +22,18 @@ let in { options.nixosConfigurations = mkOption { - type = lazyAttrsOf nixosConfiguration; + type = lazyAttrsOf (optFunctionTo nixosConfiguration); default = { }; }; config.outputs = mkIf (config.nixosConfigurations != { }) { - inherit (config) nixosConfigurations; + nixosConfigurations = mapAttrs (_: f: f autoloadArgs) + config.nixosConfigurations; checks = foldl recursiveUpdate { } (mapAttrsToList (n: v: { ${v.config.nixpkgs.system}."nixos-${n}" = v.config.system.build.toplevel; }) - config.nixosConfigurations); + (mapAttrs (_: f: f autoloadArgs) config.nixosConfigurations)); }; }