mirror of
https://github.com/nix-community/flakelight.git
synced 2025-06-21 01:57:57 +02:00
Enable simpler config of nixos/home configurations
Instead of having to call the configuration generation functions, the params can just be set, and the functions will be called automatically with additional useful settings automatically set.
This commit is contained in:
parent
ec343ae967
commit
cc2f42fbdf
79
API_GUIDE.md
79
API_GUIDE.md
@ -710,24 +710,23 @@ To write the above using autoloads, can use the following:
|
|||||||
{ hello, ... }: x: hello;
|
{ hello, ... }: x: hello;
|
||||||
```
|
```
|
||||||
|
|
||||||
### nixosConfigurations and homeConfigurations
|
### nixosConfigurations
|
||||||
|
|
||||||
The `nixosConfigurations` and `homeConfigurations` attributes let you set
|
The `nixosConfigurations` attribute lets you set outputs for NixOS systems and
|
||||||
outputs for NixOS systems and home-manager users.
|
home-manager users.
|
||||||
|
|
||||||
They should be set to an attribute set of respective configurations.
|
It should be set to an attribute set. Each value should be a set of
|
||||||
|
`nixpkgs.lib.nixosSystem` args, the result of calling `nixpkgs.lib.nixosSystem`,
|
||||||
|
or a function that takes `moduleArgs` and returns one of the prior.
|
||||||
|
|
||||||
Alternatively, the configurations can be functions, in which case those
|
When using a set of `nixpkgs.lib.nixosSystem` args, NixOS modules will have
|
||||||
functions will be passed `moduleArgs` and must return a standard
|
access to a `flake` module arg equivalent to `moduleArgs` plus `inputs'` and
|
||||||
configuration (this is useful when using autoloads with the `nixDir` feature).
|
`outputs'`. Flakelight's pkgs attributes, `withOverlays`, and `packages` will
|
||||||
|
also be available in the NixOS instance's pkgs.
|
||||||
|
|
||||||
The `propagationModule` config provides a module to apply flakelight
|
When using the result of calling `nixpkgs.lib.nixosSystem`, the
|
||||||
configuration to other module systems such as NixOS and home-manager. Applying
|
`config.propogationModule` value can be used as a NixOS module to gain the above
|
||||||
this module will give modules in the nested modules system access to a `flake`
|
benefits.
|
||||||
module arg that contains the flakelight module args as well as `inputs'` and
|
|
||||||
`outputs'`. Flakelight's packages configuration will also be applied to the pkgs
|
|
||||||
of the nested module system (This includes flakelight's additional pkgs values,
|
|
||||||
`withOverlays` overlays, and the flake's packages.
|
|
||||||
|
|
||||||
For example:
|
For example:
|
||||||
|
|
||||||
@ -736,44 +735,54 @@ For example:
|
|||||||
inputs.flakelight.url = "github:nix-community/flakelight";
|
inputs.flakelight.url = "github:nix-community/flakelight";
|
||||||
outputs = { flakelight, ... }:
|
outputs = { flakelight, ... }:
|
||||||
flakelight ./. ({ lib, config, ... }: {
|
flakelight ./. ({ lib, config, ... }: {
|
||||||
nixosConfigurations.system = lib.nixosSystem {
|
nixosConfigurations.test-system = {
|
||||||
# nixosSystem arguments
|
system = "x86_64-linux";
|
||||||
modules = [ config.propagationModule ];
|
modules = [{ system.stateVersion = "24.05"; }];
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### homeConfigurations
|
||||||
|
|
||||||
|
The `homeConfigurations` attribute lets you set outputs for NixOS systems and
|
||||||
|
home-manager users.
|
||||||
|
|
||||||
|
It should be set to an attribute set. Each value should be a set of
|
||||||
|
`home-manager.lib.homeManagerConfiguration` args, the result of calling
|
||||||
|
`home-manager.lib.homeManagerConfiguration`, or a function that takes
|
||||||
|
`moduleArgs` and returns one of the prior.
|
||||||
|
|
||||||
|
When using a set of `homeManagerConfiguration` args, it is required to include
|
||||||
|
`system` (`pkgs` does not need to be included), and `inputs.home-manager` must
|
||||||
|
be set. home-manager modules will have access to a `flake` module arg equivalent
|
||||||
|
to `moduleArgs` plus `inputs'` and `outputs'`. Flakelight's pkgs attributes,
|
||||||
|
`withOverlays`, and `packages` will also be available in the home-manager
|
||||||
|
instance's pkgs.
|
||||||
|
|
||||||
|
When using the result of calling `homeManagerConfiguration`, the
|
||||||
|
`config.propogationModule` value can be used as a home-manager module to gain
|
||||||
|
the above benefits.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
```nix
|
```nix
|
||||||
{
|
{
|
||||||
inputs = {
|
inputs = {
|
||||||
flakelight.url = "github:nix-community/flakelight";
|
flakelight.url = "github:nix-community/flakelight";
|
||||||
home-manger.url = "github:nix-community/home-manager";
|
home-manger.url = "github:nix-community/home-manager";
|
||||||
};
|
};
|
||||||
outputs = { flakelight, home-manager, ... }:
|
outputs = { flakelight, home-manager, ... }@inputs:
|
||||||
flakelight ./. ({ config, ... }: {
|
flakelight ./. ({ config, ... }: {
|
||||||
homeConfigurations.user = home-manager.lib.homeManagerConfiguration {
|
inherit inputs;
|
||||||
# homeManagerConfiguration arguments
|
homeConfigurations.username = {
|
||||||
modules = [ config.propagationModule ];
|
system = "x86_64-linux";
|
||||||
|
modules = [{ home.stateVersion = "24.05"; }];
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Optionally, defining as a function:
|
|
||||||
|
|
||||||
```nix
|
|
||||||
{
|
|
||||||
inputs.flakelight.url = "github:nix-community/flakelight";
|
|
||||||
outputs = { flakelight, ... }:
|
|
||||||
flakelight ./. {
|
|
||||||
nixosConfigurations.system = { lib, ... }: lib.nixosSystem {
|
|
||||||
# nixosSystem arguments
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### nixosModules, homeModules, and flakelightModules
|
### nixosModules, homeModules, and flakelightModules
|
||||||
|
|
||||||
The `nixosModules`, `homeModules`, and `flakelightModules` options allow you to
|
The `nixosModules`, `homeModules`, and `flakelightModules` options allow you to
|
||||||
|
@ -2,27 +2,53 @@
|
|||||||
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
|
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
|
||||||
# SPDX-License-Identifier: MIT
|
# SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
{ config, lib, flakelight, moduleArgs, ... }:
|
{ config, lib, inputs, flakelight, moduleArgs, ... }:
|
||||||
let
|
let
|
||||||
inherit (builtins) isAttrs mapAttrs;
|
inherit (builtins) concatLists head mapAttrs match;
|
||||||
inherit (lib) foldl mapAttrsToList mergeOneOption mkOption mkOptionType mkIf
|
inherit (lib) foldl last mapAttrsToList mkOption mkIf recursiveUpdate
|
||||||
recursiveUpdate;
|
zipAttrsWith;
|
||||||
inherit (lib.types) lazyAttrsOf;
|
inherit (lib.types) attrs lazyAttrsOf;
|
||||||
inherit (flakelight.types) optFunctionTo;
|
inherit (flakelight.types) optFunctionTo;
|
||||||
|
|
||||||
homeConfiguration = mkOptionType {
|
isHome = x: x ? activationPackage;
|
||||||
name = "homeConfiguration";
|
|
||||||
description = "homeConfiguration";
|
|
||||||
descriptionClass = "noun";
|
|
||||||
check = x: isAttrs x && x ? activationPackage;
|
|
||||||
merge = mergeOneOption;
|
|
||||||
};
|
|
||||||
|
|
||||||
configs = mapAttrs (_: f: f moduleArgs) config.homeConfigurations;
|
mergeCfg = zipAttrsWith (n: vs:
|
||||||
|
if n == "extraSpecialArgs" then
|
||||||
|
foldl (a: b: a // b) { } vs
|
||||||
|
else if n == "modules" then
|
||||||
|
concatLists vs
|
||||||
|
else last vs);
|
||||||
|
|
||||||
|
mkHome = name: cfg:
|
||||||
|
let
|
||||||
|
inherit (cfg) system;
|
||||||
|
in
|
||||||
|
inputs.home-manager.lib.homeManagerConfiguration (mergeCfg [
|
||||||
|
{
|
||||||
|
extraSpecialArgs = {
|
||||||
|
inherit inputs;
|
||||||
|
inputs' = mapAttrs (_: mapAttrs (_: v: v.${system} or { })) inputs;
|
||||||
|
};
|
||||||
|
modules = [
|
||||||
|
({ lib, ... }: {
|
||||||
|
home.username = lib.mkDefault (head (match "([^@]*)(@.*)?" name));
|
||||||
|
})
|
||||||
|
config.propagationModule
|
||||||
|
];
|
||||||
|
pkgs = inputs.nixpkgs.legacyPackages.${system};
|
||||||
|
}
|
||||||
|
(removeAttrs cfg [ "system" ])
|
||||||
|
]);
|
||||||
|
|
||||||
|
configs = mapAttrs
|
||||||
|
(name: f:
|
||||||
|
let val = f moduleArgs; in
|
||||||
|
if isHome val then val else mkHome name val)
|
||||||
|
config.homeConfigurations;
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
options.homeConfigurations = mkOption {
|
options.homeConfigurations = mkOption {
|
||||||
type = lazyAttrsOf (optFunctionTo homeConfiguration);
|
type = lazyAttrsOf (optFunctionTo attrs);
|
||||||
default = { };
|
default = { };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2,40 +2,60 @@
|
|||||||
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
|
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
|
||||||
# SPDX-License-Identifier: MIT
|
# SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
{ config, lib, flakelight, moduleArgs, ... }:
|
{ config, lib, inputs, flakelight, moduleArgs, ... }:
|
||||||
let
|
let
|
||||||
inherit (builtins) isAttrs mapAttrs;
|
inherit (builtins) concatLists mapAttrs;
|
||||||
inherit (lib) foldl mapAttrsToList mergeOneOption mkIf mkOption mkOptionType
|
inherit (lib) foldl last mapAttrsToList mkIf mkOption recursiveUpdate
|
||||||
recursiveUpdate;
|
zipAttrsWith;
|
||||||
inherit (lib.types) lazyAttrsOf;
|
inherit (lib.types) attrs lazyAttrsOf;
|
||||||
inherit (flakelight.types) optFunctionTo;
|
inherit (flakelight.types) optFunctionTo;
|
||||||
|
|
||||||
nixosConfiguration = mkOptionType {
|
# Avoid checking if toplevel is a derivation as it causes the nixos modules
|
||||||
name = "nixosConfiguration";
|
# to be evaluated.
|
||||||
description = "nixosConfiguration";
|
isNixos = x: x ? config.system.build.toplevel;
|
||||||
descriptionClass = "noun";
|
|
||||||
check = x: isAttrs x
|
|
||||||
&& x ? config.nixpkgs.system
|
|
||||||
&& x ? config.system.build.toplevel;
|
|
||||||
merge = mergeOneOption;
|
|
||||||
};
|
|
||||||
|
|
||||||
configs = mapAttrs (_: f: f moduleArgs) config.nixosConfigurations;
|
mergeCfg = zipAttrsWith (n: vs:
|
||||||
|
if n == "specialArgs" then
|
||||||
|
foldl (a: b: a // b) { } vs
|
||||||
|
else if n == "modules" then
|
||||||
|
concatLists vs
|
||||||
|
else last vs);
|
||||||
|
|
||||||
|
mkSystem = hostname: cfg:
|
||||||
|
let
|
||||||
|
inherit (cfg) system;
|
||||||
|
in
|
||||||
|
inputs.nixpkgs.lib.nixosSystem (mergeCfg [
|
||||||
|
{
|
||||||
|
specialArgs = {
|
||||||
|
inherit inputs hostname;
|
||||||
|
inputs' = mapAttrs (_: mapAttrs (_: v: v.${system} or { })) inputs;
|
||||||
|
};
|
||||||
|
modules = [ config.propagationModule ];
|
||||||
|
}
|
||||||
|
cfg
|
||||||
|
]);
|
||||||
|
|
||||||
|
systems = mapAttrs
|
||||||
|
(hostname: f:
|
||||||
|
let val = f moduleArgs; in
|
||||||
|
if isNixos val then val else mkSystem hostname val)
|
||||||
|
config.nixosConfigurations;
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
options.nixosConfigurations = mkOption {
|
options.nixosConfigurations = mkOption {
|
||||||
type = lazyAttrsOf (optFunctionTo nixosConfiguration);
|
type = lazyAttrsOf (optFunctionTo attrs);
|
||||||
default = { };
|
default = { };
|
||||||
};
|
};
|
||||||
|
|
||||||
config.outputs = mkIf (config.nixosConfigurations != { }) {
|
config.outputs = mkIf (config.nixosConfigurations != { }) {
|
||||||
nixosConfigurations = configs;
|
nixosConfigurations = systems;
|
||||||
checks = foldl recursiveUpdate { } (mapAttrsToList
|
checks = foldl recursiveUpdate { } (mapAttrsToList
|
||||||
(n: v: {
|
(n: v: {
|
||||||
${v.config.nixpkgs.system}."nixos-${n}" = v.pkgs.runCommand
|
${v.config.nixpkgs.system}."nixos-${n}" = v.pkgs.runCommand
|
||||||
"check-nixos-${n}"
|
"check-nixos-${n}"
|
||||||
{ } "echo ${v.config.system.build.toplevel} > $out";
|
{ } "echo ${v.config.system.build.toplevel} > $out";
|
||||||
})
|
})
|
||||||
configs);
|
systems);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -589,14 +589,23 @@ in
|
|||||||
|
|
||||||
nixosConfigurations = test
|
nixosConfigurations = test
|
||||||
(flakelight ./empty ({ lib, ... }: {
|
(flakelight ./empty ({ lib, ... }: {
|
||||||
nixosConfigurations.test = nixpkgs.lib.nixosSystem {
|
nixosConfigurations.test = {
|
||||||
system = "x86_64-linux";
|
system = "x86_64-linux";
|
||||||
modules = [ ];
|
modules = [{ system.stateVersion = "24.05"; }];
|
||||||
};
|
};
|
||||||
}))
|
}))
|
||||||
(f: f ? nixosConfigurations.test.config.system.build.toplevel);
|
(f: f ? nixosConfigurations.test.config.system.build.toplevel);
|
||||||
|
|
||||||
nixosConfigurationsWithProp = test
|
nixosConfigurationsManual = test
|
||||||
|
(flakelight ./empty ({ lib, ... }: {
|
||||||
|
nixosConfigurations.test = nixpkgs.lib.nixosSystem {
|
||||||
|
system = "x86_64-linux";
|
||||||
|
modules = [{ system.stateVersion = "24.05"; }];
|
||||||
|
};
|
||||||
|
}))
|
||||||
|
(f: f ? nixosConfigurations.test.config.system.build.toplevel);
|
||||||
|
|
||||||
|
nixosConfigurationsManualWithProp = test
|
||||||
(flakelight ./empty ({ lib, config, ... }: {
|
(flakelight ./empty ({ lib, config, ... }: {
|
||||||
nixosConfigurations.test = nixpkgs.lib.nixosSystem {
|
nixosConfigurations.test = nixpkgs.lib.nixosSystem {
|
||||||
system = "x86_64-linux";
|
system = "x86_64-linux";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user