style: fmt

This commit is contained in:
anntnzrb
2025-10-09 00:06:31 -05:00
parent e0be5043e5
commit 7d7ff85b41
14 changed files with 1008 additions and 987 deletions

View File

@@ -90,7 +90,8 @@ in
## ``` ## ```
## Result: Merged items with aliases applied ## Result: Merged items with aliases applied
#@ (Attrs -> Attrs -> Attrs) -> [Attrs] -> Attrs -> Attrs #@ (Attrs -> Attrs -> Attrs) -> [Attrs] -> Attrs -> Attrs
merge-with-aliases = merge-fn: items: alias: merge-with-aliases =
merge-fn: items: alias:
let let
merged = foldl merge-fn { } items; merged = foldl merge-fn { } items;
in in

View File

@@ -16,7 +16,7 @@
## { another-check = ...; my-check = ...; default = ...; } ## { another-check = ...; my-check = ...; default = ...; }
## ``` ## ```
#@ Attrs -> Attrs #@ Attrs -> Attrs
create-checks = args: create-checks =
snowfall-lib.internal.create-simple-derivations (args // { type = "checks"; }); args: snowfall-lib.internal.create-simple-derivations (args // { type = "checks"; });
}; };
} }

View File

@@ -50,15 +50,13 @@ let
# result: { x = nixpkgs.lib; } # result: { x = nixpkgs.lib; }
get-libs = get-libs =
attrs: attrs:
fold fold (
(name: acc: name: acc:
let value = attrs.${name}; in let
if builtins.isAttrs (value.lib or null) value = attrs.${name};
then acc // { ${name} = value.lib; } in
else acc if builtins.isAttrs (value.lib or null) then acc // { ${name} = value.lib; } else acc
) ) { } (builtins.attrNames attrs);
{ }
(builtins.attrNames attrs);
# Remove the `self` attribute from an attribute set. # Remove the `self` attribute from an attribute set.
# Type: Attrs -> Attrs # Type: Attrs -> Attrs

View File

@@ -20,91 +20,93 @@ let
; ;
in in
let let
flake = let flake =
## Remove the `self` attribute from an attribute set. let
## Example Usage: ## Remove the `self` attribute from an attribute set.
## ```nix ## Example Usage:
## without-self { self = {}; x = true; } ## ```nix
## ``` ## without-self { self = {}; x = true; }
## Result: ## ```
## ```nix ## Result:
## { x = true; } ## ```nix
## ``` ## { x = true; }
#@ Attrs -> Attrs ## ```
without-self = flake-inputs: builtins.removeAttrs flake-inputs [ "self" ]; #@ Attrs -> Attrs
without-self = flake-inputs: builtins.removeAttrs flake-inputs [ "self" ];
## Remove the `src` attribute from an attribute set. ## Remove the `src` attribute from an attribute set.
## Example Usage: ## Example Usage:
## ```nix ## ```nix
## without-src { src = ./.; x = true; } ## without-src { src = ./.; x = true; }
## ``` ## ```
## Result: ## Result:
## ```nix ## ```nix
## { x = true; } ## { x = true; }
## ``` ## ```
#@ Attrs -> Attrs #@ Attrs -> Attrs
without-src = flake-inputs: builtins.removeAttrs flake-inputs [ "src" ]; without-src = flake-inputs: builtins.removeAttrs flake-inputs [ "src" ];
in { in
inherit without-self without-src; {
inherit without-self without-src;
## Remove the `src` and `self` attributes from an attribute set. ## Remove the `src` and `self` attributes from an attribute set.
## Example Usage: ## Example Usage:
## ```nix ## ```nix
## without-snowfall-inputs { self = {}; src = ./.; x = true; } ## without-snowfall-inputs { self = {}; src = ./.; x = true; }
## ``` ## ```
## Result: ## Result:
## ```nix ## ```nix
## { x = true; } ## { x = true; }
## ``` ## ```
#@ Attrs -> Attrs #@ Attrs -> Attrs
without-snowfall-inputs = snowfall-lib.fp.compose without-self without-src; without-snowfall-inputs = snowfall-lib.fp.compose without-self without-src;
## Remove Snowfall-specific attributes so the rest can be safely passed to flake-utils-plus. ## Remove Snowfall-specific attributes so the rest can be safely passed to flake-utils-plus.
## Example Usage: ## Example Usage:
## ```nix ## ```nix
## without-snowfall-options { src = ./.; x = true; } ## without-snowfall-options { src = ./.; x = true; }
## ``` ## ```
## Result: ## Result:
## ```nix ## ```nix
## { x = true; } ## { x = true; }
## ``` ## ```
#@ Attrs -> Attrs #@ Attrs -> Attrs
without-snowfall-options = without-snowfall-options =
flake-options: flake-options:
builtins.removeAttrs flake-options [ builtins.removeAttrs flake-options [
"systems" "systems"
"modules" "modules"
"overlays" "overlays"
"packages" "packages"
"outputs-builder" "outputs-builder"
"outputsBuilder" "outputsBuilder"
"packagesPrefix" "packagesPrefix"
"hosts" "hosts"
"homes" "homes"
"channels-config" "channels-config"
"templates" "templates"
"checks" "checks"
"alias" "alias"
"snowfall" "snowfall"
]; ];
## Transform an attribute set of inputs into an attribute set where the values are the inputs' `lib` attribute. Entries without a `lib` attribute are removed. ## Transform an attribute set of inputs into an attribute set where the values are the inputs' `lib` attribute. Entries without a `lib` attribute are removed.
## Example Usage: ## Example Usage:
## ```nix ## ```nix
## get-lib { x = nixpkgs; y = {}; } ## get-lib { x = nixpkgs; y = {}; }
## ``` ## ```
## Result: ## Result:
## ```nix ## ```nix
## { x = nixpkgs.lib; } ## { x = nixpkgs.lib; }
## ``` ## ```
#@ Attrs -> Attrs #@ Attrs -> Attrs
get-libs = get-libs =
attrs: attrs:
pipe attrs [ pipe attrs [
(filterAttrs (name: value: builtins.isAttrs (value.lib or null))) (filterAttrs (name: value: builtins.isAttrs (value.lib or null)))
(mapAttrs (name: value: value.lib)) (mapAttrs (name: value: value.lib))
]; ];
}; };
mkFlake = mkFlake =
full-flake-options: full-flake-options:
@@ -227,9 +229,7 @@ let
(mapAttrs (home-name: _: flake-outputs.homeConfigurations.${home-name})) (mapAttrs (home-name: _: flake-outputs.homeConfigurations.${home-name}))
(mapAttrs' ( (mapAttrs' (
name: value: name: value:
nameValuePair nameValuePair (if hasSuffix "@${system}" name then removeSuffix "@${system}" name else name) value
(if hasSuffix "@${system}" name then removeSuffix "@${system}" name else name)
value
)) ))
]; ];
}; };

View File

@@ -9,57 +9,59 @@ let
inherit (core-inputs.nixpkgs.lib) id foldr flip; inherit (core-inputs.nixpkgs.lib) id foldr flip;
in in
{ {
fp = let fp =
## Compose two functions. let
## Example Usage: ## Compose two functions.
## ```nix ## Example Usage:
## compose add-two add-one ## ```nix
## ``` ## compose add-two add-one
## Result: ## ```
## ```nix ## Result:
## (x: add-two (add-one x)) ## ```nix
## ``` ## (x: add-two (add-one x))
#@ (b -> c) -> (a -> b) -> a -> c ## ```
compose = #@ (b -> c) -> (a -> b) -> a -> c
f: g: x: compose =
f (g x); f: g: x:
f (g x);
## Call a function with an argument. ## Call a function with an argument.
## Example Usage: ## Example Usage:
## ```nix ## ```nix
## call (x: x + 1) 0 ## call (x: x + 1) 0
## ``` ## ```
## Result: ## Result:
## ```nix ## ```nix
## 1 ## 1
## ``` ## ```
#@ (a -> b) -> a -> b #@ (a -> b) -> a -> b
call = f: x: f x; call = f: x: f x;
in { in
inherit compose call; {
inherit compose call;
## Compose many functions. ## Compose many functions.
## Example Usage: ## Example Usage:
## ```nix ## ```nix
## compose-all [ add-two add-one ] ## compose-all [ add-two add-one ]
## ``` ## ```
## Result: ## Result:
## ```nix ## ```nix
## (x: add-two (add-one x)) ## (x: add-two (add-one x))
## ``` ## ```
#@ [(x -> y)] -> a -> b #@ [(x -> y)] -> a -> b
compose-all = foldr compose id; compose-all = foldr compose id;
## Apply an argument to a function. ## Apply an argument to a function.
## Example Usage: ## Example Usage:
## ```nix ## ```nix
## apply 0 (x: x + 1) ## apply 0 (x: x + 1)
## ``` ## ```
## Result: ## Result:
## ```nix ## ```nix
## 1 ## 1
## ``` ## ```
#@ a -> (a -> b) -> b #@ a -> (a -> b) -> b
apply = flip call; apply = flip call;
}; };
} }

View File

@@ -122,8 +122,7 @@ in
## ``` ## ```
#@ Path -> [Path] #@ Path -> [Path]
get-directories-with-default = get-directories-with-default =
path: path: builtins.filter (dir: pathExists "${dir}/default.nix") (get-directories path);
builtins.filter (dir: pathExists "${dir}/default.nix") (get-directories path);
## Get files at a given path. ## Get files at a given path.
## Example Usage: ## Example Usage:
@@ -234,7 +233,9 @@ in
## [ "./something/some-directory/default.nix" ] ## [ "./something/some-directory/default.nix" ]
## ``` ## ```
#@ Path -> [Path] #@ Path -> [Path]
get-default-nix-files-recursive = filter-files-recursive (f: builtins.baseNameOf f == "default.nix"); get-default-nix-files-recursive = filter-files-recursive (
f: builtins.baseNameOf f == "default.nix"
);
## Get nix files at a given path not named "default.nix". ## Get nix files at a given path not named "default.nix".
## Example Usage: ## Example Usage:
@@ -246,8 +247,9 @@ in
## [ "./something/a.nix" ] ## [ "./something/a.nix" ]
## ``` ## ```
#@ Path -> [Path] #@ Path -> [Path]
get-non-default-nix-files = filter-files (f: get-non-default-nix-files = filter-files (
snowfall-lib.path.has-file-extension "nix" f && builtins.baseNameOf f != "default.nix"); f: snowfall-lib.path.has-file-extension "nix" f && builtins.baseNameOf f != "default.nix"
);
## Get nix files at a given path not named "default.nix", traversing any directories within. ## Get nix files at a given path not named "default.nix", traversing any directories within.
## Example Usage: ## Example Usage:
@@ -259,7 +261,8 @@ in
## [ "./something/some-directory/a.nix" ] ## [ "./something/some-directory/a.nix" ]
## ``` ## ```
#@ Path -> [Path] #@ Path -> [Path]
get-non-default-nix-files-recursive = filter-files-recursive (f: get-non-default-nix-files-recursive = filter-files-recursive (
snowfall-lib.path.has-file-extension "nix" f && builtins.baseNameOf f != "default.nix"); f: snowfall-lib.path.has-file-extension "nix" f && builtins.baseNameOf f != "default.nix"
);
}; };
} }

View File

@@ -33,409 +33,411 @@ let
user-modules-root = snowfall-lib.fs.get-snowfall-file "modules"; user-modules-root = snowfall-lib.fs.get-snowfall-file "modules";
in in
{ {
home = let home =
# Modules in home-manager expect `hm` to be available directly on `lib` itself. let
home-lib = # Modules in home-manager expect `hm` to be available directly on `lib` itself.
# NOTE: This prevents an error during evaluation if the input does home-lib =
# not exist. # NOTE: This prevents an error during evaluation if the input does
if user-inputs ? home-manager then # not exist.
snowfall-lib.internal.system-lib.extend ( if user-inputs ? home-manager then
final: prev: snowfall-lib.internal.system-lib.extend (
# NOTE: This order is important, this library's extend and other utilities must write final: prev:
# _over_ the original `system-lib`. # NOTE: This order is important, this library's extend and other utilities must write
snowfall-lib.internal.system-lib # _over_ the original `system-lib`.
// prev snowfall-lib.internal.system-lib
// { // prev
hm = snowfall-lib.internal.system-lib.home-manager.hm; // {
} hm = snowfall-lib.internal.system-lib.home-manager.hm;
) }
else )
{ }; else
{ };
## Get the user and host from a combined string. ## Get the user and host from a combined string.
## Example Usage: ## Example Usage:
## ```nix ## ```nix
## split-user-and-host "myuser@myhost" ## split-user-and-host "myuser@myhost"
## ``` ## ```
## Result: ## Result:
## ```nix ## ```nix
## { user = "myuser"; host = "myhost"; } ## { user = "myuser"; host = "myhost"; }
## ``` ## ```
#@ String -> Attrs #@ String -> Attrs
split-user-and-host = split-user-and-host =
target: target:
let let
name-parts = pipe target [ name-parts = pipe target [
(builtins.split "@") (builtins.split "@")
(builtins.filter builtins.isString) (builtins.filter builtins.isString)
]; ];
user = builtins.elemAt name-parts 0; user = builtins.elemAt name-parts 0;
host = if builtins.length name-parts > 1 then builtins.elemAt name-parts 1 else ""; host = if builtins.length name-parts > 1 then builtins.elemAt name-parts 1 else "";
in in
{ {
inherit user host; inherit user host;
};
## Create a home.
## Example Usage:
## ```nix
## create-home { path = ./homes/my-home; }
## ```
## Result:
## ```nix
## <flake-utils-plus-home-configuration>
## ```
#@ Attrs -> Attrs
create-home =
{
path,
name ? builtins.unsafeDiscardStringContext (snowfall-lib.system.get-inferred-system-name path),
modules ? [ ],
specialArgs ? { },
channelName ? "nixpkgs",
system ? "x86_64-linux",
}:
let
user-metadata = split-user-and-host name;
unique-name = if user-metadata.host == "" then "${user-metadata.user}@${system}" else name;
# NOTE: home-manager has trouble with `pkgs` recursion if it isn't passed in here.
pkgs = user-inputs.self.pkgs.${system}.${channelName} // {
lib = home-lib;
}; };
lib = home-lib;
in
assert assertMsg (user-inputs ? home-manager)
"In order to create home-manager configurations, you must include `home-manager` as a flake input.";
assert assertMsg (
(user-metadata.host != "") || !(hasInfix "@" name)
) "Snowfall Lib homes must be named with the format: user@system";
{
inherit channelName system;
output = "homeConfigurations"; ## Create a home.
## Example Usage:
modules = [ ## ```nix
path ## create-home { path = ./homes/my-home; }
../../modules/home/user/default.nix ## ```
] ## Result:
++ modules; ## ```nix
## <flake-utils-plus-home-configuration>
specialArgs = { ## ```
inherit system; #@ Attrs -> Attrs
name = unique-name; create-home =
inherit (user-metadata) user host; {
path,
format = "home"; name ? builtins.unsafeDiscardStringContext (snowfall-lib.system.get-inferred-system-name path),
modules ? [ ],
inputs = snowfall-lib.flake.without-src user-inputs; specialArgs ? { },
namespace = snowfall-config.namespace; channelName ? "nixpkgs",
system ? "x86_64-linux",
}:
let
user-metadata = split-user-and-host name;
unique-name = if user-metadata.host == "" then "${user-metadata.user}@${system}" else name;
# NOTE: home-manager has trouble with `pkgs` recursion if it isn't passed in here. # NOTE: home-manager has trouble with `pkgs` recursion if it isn't passed in here.
inherit pkgs lib; pkgs = user-inputs.self.pkgs.${system}.${channelName} // {
}; lib = home-lib;
builder =
args:
user-inputs.home-manager.lib.homeManagerConfiguration (
(builtins.removeAttrs args [
"system"
"specialArgs"
])
// {
inherit pkgs lib;
modules = args.modules ++ [
(
module-args:
import ./nix-registry-module.nix (
module-args
// {
inherit user-inputs core-inputs;
}
)
)
{
snowfallorg.user = {
name = mkDefault user-metadata.user;
enable = mkDefault true;
};
}
];
extraSpecialArgs = specialArgs // args.specialArgs;
}
);
};
## Get structured data about all homes for a given target.
## Example Usage:
## ```nix
## get-target-homes-metadata ./homes
## ```
## Result:
## ```nix
## [ { system = "x86_64-linux"; name = "my-home"; path = "/homes/x86_64-linux/my-home";} ]
## ```
#@ String -> [Attrs]
get-target-homes-metadata =
target:
let
existing-homes = snowfall-lib.fs.get-directories-with-default target;
create-home-metadata =
path:
let
# We are building flake outputs based on file contents. Nix doesn't like this
# so we have to explicitly discard the string's path context to allow us to
# use the name as a variable.
basename = builtins.unsafeDiscardStringContext (builtins.baseNameOf path);
# We are building flake outputs based on file contents. Nix doesn't like this
# so we have to explicitly discard the string's path context to allow us to
# use the name as a variable.
system = builtins.unsafeDiscardStringContext (builtins.baseNameOf target);
name = if !(hasInfix "@" basename) then "${basename}@${system}" else basename;
in
{
path = "${path}/default.nix";
inherit name system;
}; };
home-configurations = builtins.map create-home-metadata existing-homes; lib = home-lib;
in in
home-configurations; assert assertMsg (user-inputs ? home-manager)
"In order to create home-manager configurations, you must include `home-manager` as a flake input.";
assert assertMsg (
(user-metadata.host != "") || !(hasInfix "@" name)
) "Snowfall Lib homes must be named with the format: user@system";
{
inherit channelName system;
## Create all available homes. output = "homeConfigurations";
## Example Usage:
## ```nix
## create-homes { users."my-user@my-system".specialArgs.x = true; modules = [ my-shared-module ]; }
## ```
## Result:
## ```nix
## { "my-user@my-system" = <flake-utils-plus-home-configuration>; }
## ```
#@ Attrs -> Attrs
create-homes =
homes:
let
targets = snowfall-lib.fs.get-directories user-homes-root;
target-homes-metadata = concatMap get-target-homes-metadata targets;
user-home-modules = snowfall-lib.module.create-modules { modules = [
src = "${user-modules-root}/home"; path
}; ../../modules/home/user/default.nix
]
++ modules;
user-home-modules-list = mapAttrsToList ( specialArgs = {
module-path: module: inherit system;
args@{ pkgs, ... }: name = unique-name;
(module args) inherit (user-metadata) user host;
// {
_file = "${user-homes-root}/${module-path}/default.nix";
}
) user-home-modules;
create-home' = format = "home";
home-metadata:
let inputs = snowfall-lib.flake.without-src user-inputs;
inherit (home-metadata) name; namespace = snowfall-config.namespace;
overrides = homes.users.${name} or { };
in # NOTE: home-manager has trouble with `pkgs` recursion if it isn't passed in here.
{ inherit pkgs lib;
"${name}" = create-home ( };
overrides
// home-metadata builder =
args:
user-inputs.home-manager.lib.homeManagerConfiguration (
(builtins.removeAttrs args [
"system"
"specialArgs"
])
// { // {
modules = user-home-modules-list ++ (homes.users.${name}.modules or [ ]) ++ (homes.modules or [ ]); inherit pkgs lib;
}
);
};
created-homes = foldl ( modules = args.modules ++ [
homes: home-metadata: homes // (create-home' home-metadata) (
) { } target-homes-metadata; module-args:
in import ./nix-registry-module.nix (
created-homes; module-args
## Create system modules for home-manager integration.
## Example Usage:
## ```nix
## create-home-system-modules { users."my-user@my-system".specialArgs.x = true; modules = [ my-shared-module ]; }
## ```
## Result:
## ```nix
## [Module]
## ```
#@ Attrs -> [Module]
create-home-system-modules =
users:
let
created-users = create-homes users;
user-home-modules = snowfall-lib.module.create-modules {
src = "${user-modules-root}/home";
};
shared-modules = builtins.map (module: {
config.home-manager.sharedModules = [ module ];
}) (users.modules or [ ]);
shared-user-modules = mapAttrsToList (module-path: module: {
_file = "${user-modules-root}/home/${module-path}/default.nix";
config = {
home-manager.sharedModules = [ module ];
};
}) user-home-modules;
snowfall-user-home-module = {
_file = "virtual:snowfallorg/modules/home/user/default.nix";
config = {
home-manager.sharedModules = [
../../modules/home/user/default.nix
];
};
};
extra-special-args-module =
args@{
config,
pkgs,
system ? pkgs.stdenv.hostPlatform.system,
target ? system,
format ? "home",
host ? "",
virtual ? (snowfall-lib.system.is-virtual target),
systems ? { },
...
}:
{
_file = "virtual:snowfallorg/home/extra-special-args";
config = {
home-manager.extraSpecialArgs = {
inherit
system
target
format
virtual
systems
host
;
inherit (snowfall-config) namespace;
lib = home-lib;
inputs = snowfall-lib.flake.without-src user-inputs;
};
};
};
system-modules = builtins.map (
name:
let
created-user = created-users.${name};
user-module = head created-user.modules;
other-modules = users.users.${name}.modules or [ ];
user-name = created-user.specialArgs.user;
in
args@{
config,
options,
pkgs,
host ? "",
system ? pkgs.stdenv.hostPlatform.system,
...
}:
let
host-matches = (name == "${user-name}@${host}") || (name == "${user-name}@${system}");
# NOTE: To conform to the config structure of home-manager, we have to
# remap the options coming from `snowfallorg.user.<name>.home.config` since `mkAliasDefinitions`
# does not let us target options within a submodule.
wrap-user-options =
user-option:
if (user-option ? "_type") && user-option._type == "merge" then
user-option
// {
contents = builtins.map (
merge-entry: merge-entry.${user-name}.home.config or { }
) user-option.contents;
}
else
(builtins.trace ''
=============
Snowfall Lib:
Option value for `snowfallorg.users.${user-name}` was not detected to be merged.
Please report the issue on GitHub with a link to your configuration so we can debug the problem:
https://github.com/snowfallorg/lib/issues/new
=============
'')
user-option;
home-config = mkAliasAndWrapDefinitions wrap-user-options options.snowfallorg.users;
in
{
_file = "virtual:snowfallorg/home/user/${name}";
config = mkIf host-matches {
# Initialize user information.
snowfallorg.users.${user-name}.home.config = {
snowfallorg.user = {
enable = mkDefault true;
name = mkDefault user-name;
};
# NOTE: specialArgs are not propagated by Home-Manager without this.
# However, not all specialArgs values can be set when using `_module.args`.
_module.args =
builtins.removeAttrs
(
(users.users.${name}.specialArgs or { })
// { // {
namespace = snowfall-config.namespace; inherit user-inputs core-inputs;
} }
) )
[ )
"options"
"config"
"lib"
"pkgs"
"specialArgs"
"host"
];
};
home-manager = {
users.${user-name} = mkIf config.snowfallorg.users.${user-name}.home.enable (
{ pkgs, ... }:
{ {
imports = (home-config.imports or [ ]) ++ other-modules ++ [ user-module ]; snowfallorg.user = {
config = builtins.removeAttrs home-config [ "imports" ]; name = mkDefault user-metadata.user;
enable = mkDefault true;
};
} }
); ];
# NOTE: Without this home-manager will instead create its own package set which won't contain the same config and extraSpecialArgs = specialArgs // args.specialArgs;
# user-defined packages/overlays as the flake's nixpkgs channel. }
useGlobalPkgs = mkDefault true; );
};
## Get structured data about all homes for a given target.
## Example Usage:
## ```nix
## get-target-homes-metadata ./homes
## ```
## Result:
## ```nix
## [ { system = "x86_64-linux"; name = "my-home"; path = "/homes/x86_64-linux/my-home";} ]
## ```
#@ String -> [Attrs]
get-target-homes-metadata =
target:
let
existing-homes = snowfall-lib.fs.get-directories-with-default target;
create-home-metadata =
path:
let
# We are building flake outputs based on file contents. Nix doesn't like this
# so we have to explicitly discard the string's path context to allow us to
# use the name as a variable.
basename = builtins.unsafeDiscardStringContext (builtins.baseNameOf path);
# We are building flake outputs based on file contents. Nix doesn't like this
# so we have to explicitly discard the string's path context to allow us to
# use the name as a variable.
system = builtins.unsafeDiscardStringContext (builtins.baseNameOf target);
name = if !(hasInfix "@" basename) then "${basename}@${system}" else basename;
in
{
path = "${path}/default.nix";
inherit name system;
};
home-configurations = builtins.map create-home-metadata existing-homes;
in
home-configurations;
## Create all available homes.
## Example Usage:
## ```nix
## create-homes { users."my-user@my-system".specialArgs.x = true; modules = [ my-shared-module ]; }
## ```
## Result:
## ```nix
## { "my-user@my-system" = <flake-utils-plus-home-configuration>; }
## ```
#@ Attrs -> Attrs
create-homes =
homes:
let
targets = snowfall-lib.fs.get-directories user-homes-root;
target-homes-metadata = concatMap get-target-homes-metadata targets;
user-home-modules = snowfall-lib.module.create-modules {
src = "${user-modules-root}/home";
};
user-home-modules-list = mapAttrsToList (
module-path: module:
args@{ pkgs, ... }:
(module args)
// {
_file = "${user-homes-root}/${module-path}/default.nix";
}
) user-home-modules;
create-home' =
home-metadata:
let
inherit (home-metadata) name;
overrides = homes.users.${name} or { };
in
{
"${name}" = create-home (
overrides
// home-metadata
// {
modules = user-home-modules-list ++ (homes.users.${name}.modules or [ ]) ++ (homes.modules or [ ]);
}
);
};
created-homes = foldl (
homes: home-metadata: homes // (create-home' home-metadata)
) { } target-homes-metadata;
in
created-homes;
## Create system modules for home-manager integration.
## Example Usage:
## ```nix
## create-home-system-modules { users."my-user@my-system".specialArgs.x = true; modules = [ my-shared-module ]; }
## ```
## Result:
## ```nix
## [Module]
## ```
#@ Attrs -> [Module]
create-home-system-modules =
users:
let
created-users = create-homes users;
user-home-modules = snowfall-lib.module.create-modules {
src = "${user-modules-root}/home";
};
shared-modules = builtins.map (module: {
config.home-manager.sharedModules = [ module ];
}) (users.modules or [ ]);
shared-user-modules = mapAttrsToList (module-path: module: {
_file = "${user-modules-root}/home/${module-path}/default.nix";
config = {
home-manager.sharedModules = [ module ];
};
}) user-home-modules;
snowfall-user-home-module = {
_file = "virtual:snowfallorg/modules/home/user/default.nix";
config = {
home-manager.sharedModules = [
../../modules/home/user/default.nix
];
};
};
extra-special-args-module =
args@{
config,
pkgs,
system ? pkgs.stdenv.hostPlatform.system,
target ? system,
format ? "home",
host ? "",
virtual ? (snowfall-lib.system.is-virtual target),
systems ? { },
...
}:
{
_file = "virtual:snowfallorg/home/extra-special-args";
config = {
home-manager.extraSpecialArgs = {
inherit
system
target
format
virtual
systems
host
;
inherit (snowfall-config) namespace;
lib = home-lib;
inputs = snowfall-lib.flake.without-src user-inputs;
};
}; };
}; };
}
) (builtins.attrNames created-users); system-modules = builtins.map (
in name:
[ let
extra-special-args-module created-user = created-users.${name};
snowfall-user-home-module user-module = head created-user.modules;
] other-modules = users.users.${name}.modules or [ ];
++ shared-modules user-name = created-user.specialArgs.user;
++ shared-user-modules in
++ system-modules; args@{
in { config,
inherit options,
home-lib pkgs,
split-user-and-host host ? "",
create-home system ? pkgs.stdenv.hostPlatform.system,
get-target-homes-metadata ...
create-homes }:
create-home-system-modules let
; host-matches = (name == "${user-name}@${host}") || (name == "${user-name}@${system}");
};
# NOTE: To conform to the config structure of home-manager, we have to
# remap the options coming from `snowfallorg.user.<name>.home.config` since `mkAliasDefinitions`
# does not let us target options within a submodule.
wrap-user-options =
user-option:
if (user-option ? "_type") && user-option._type == "merge" then
user-option
// {
contents = builtins.map (
merge-entry: merge-entry.${user-name}.home.config or { }
) user-option.contents;
}
else
(builtins.trace ''
=============
Snowfall Lib:
Option value for `snowfallorg.users.${user-name}` was not detected to be merged.
Please report the issue on GitHub with a link to your configuration so we can debug the problem:
https://github.com/snowfallorg/lib/issues/new
=============
'')
user-option;
home-config = mkAliasAndWrapDefinitions wrap-user-options options.snowfallorg.users;
in
{
_file = "virtual:snowfallorg/home/user/${name}";
config = mkIf host-matches {
# Initialize user information.
snowfallorg.users.${user-name}.home.config = {
snowfallorg.user = {
enable = mkDefault true;
name = mkDefault user-name;
};
# NOTE: specialArgs are not propagated by Home-Manager without this.
# However, not all specialArgs values can be set when using `_module.args`.
_module.args =
builtins.removeAttrs
(
(users.users.${name}.specialArgs or { })
// {
namespace = snowfall-config.namespace;
}
)
[
"options"
"config"
"lib"
"pkgs"
"specialArgs"
"host"
];
};
home-manager = {
users.${user-name} = mkIf config.snowfallorg.users.${user-name}.home.enable (
{ pkgs, ... }:
{
imports = (home-config.imports or [ ]) ++ other-modules ++ [ user-module ];
config = builtins.removeAttrs home-config [ "imports" ];
}
);
# NOTE: Without this home-manager will instead create its own package set which won't contain the same config and
# user-defined packages/overlays as the flake's nixpkgs channel.
useGlobalPkgs = mkDefault true;
};
};
}
) (builtins.attrNames created-users);
in
[
extra-special-args-module
snowfall-user-home-module
]
++ shared-modules
++ shared-user-modules
++ system-modules;
in
{
inherit
home-lib
split-user-and-host
create-home
get-target-homes-metadata
create-homes
create-home-system-modules
;
};
} }

View File

@@ -66,36 +66,37 @@ in
internal = { internal = {
inherit system-lib user-lib; inherit system-lib user-lib;
create-simple-derivations = { create-simple-derivations =
type, {
channels, type,
src ? snowfall-lib.fs.get-snowfall-file type, channels,
pkgs ? channels.nixpkgs, src ? snowfall-lib.fs.get-snowfall-file type,
overrides ? {}, pkgs ? channels.nixpkgs,
alias ? {}, overrides ? { },
}: alias ? { },
let }:
user-items = snowfall-lib.fs.get-default-nix-files-recursive src; let
user-items = snowfall-lib.fs.get-default-nix-files-recursive src;
create-metadata = item: {
name = snowfall-lib.path.get-output-name item; create-metadata = item: {
drv = callPackageWith ( name = snowfall-lib.path.get-output-name item;
pkgs // { drv = callPackageWith (
inherit channels; pkgs
lib = system-lib; // {
inputs = snowfall-lib.flake.without-src user-inputs; inherit channels;
namespace = snowfall-config.namespace; lib = system-lib;
} inputs = snowfall-lib.flake.without-src user-inputs;
) item {}; namespace = snowfall-config.namespace;
}; }
) item { };
items-metadata = builtins.map create-metadata user-items; };
merge-items = items: metadata: items-metadata = builtins.map create-metadata user-items;
items // { ${metadata.name} = metadata.drv; };
merge-items = items: metadata: items // { ${metadata.name} = metadata.drv; };
items = snowfall-lib.attrs.merge-with-aliases merge-items items-metadata alias // overrides;
in items = snowfall-lib.attrs.merge-with-aliases merge-items items-metadata alias // overrides;
in
filterPackages pkgs.stdenv.hostPlatform.system items; filterPackages pkgs.stdenv.hostPlatform.system items;
}; };
} }

View File

@@ -5,7 +5,13 @@
snowfall-config, snowfall-config,
}: }:
let let
inherit (core-inputs.nixpkgs.lib) assertMsg foldl concatStringsSep pipe flatten; inherit (core-inputs.nixpkgs.lib)
assertMsg
foldl
concatStringsSep
pipe
flatten
;
user-overlays-root = snowfall-lib.fs.get-snowfall-file "overlays"; user-overlays-root = snowfall-lib.fs.get-snowfall-file "overlays";
user-packages-root = snowfall-lib.fs.get-snowfall-file "packages"; user-packages-root = snowfall-lib.fs.get-snowfall-file "packages";
@@ -191,12 +197,14 @@ in
default-overlay = default-overlay =
final: prev: final: prev:
pipe [ package-overlays overlays ] [ pipe
(builtins.map builtins.attrValues) [ package-overlays overlays ]
flatten [
(builtins.map (overlay: overlay final prev)) (builtins.map builtins.attrValues)
snowfall-lib.attrs.merge-shallow-packages flatten
]; (builtins.map (overlay: overlay final prev))
snowfall-lib.attrs.merge-shallow-packages
];
in in
package-overlays // overlays // { default = default-overlay; } // extra-overlays; package-overlays // overlays // { default = default-overlay; } // extra-overlays;
}; };

View File

@@ -18,78 +18,80 @@ let
user-packages-root = snowfall-lib.fs.get-snowfall-file "packages"; user-packages-root = snowfall-lib.fs.get-snowfall-file "packages";
in in
{ {
package = let package =
## Create flake output packages. let
## Example Usage: ## Create flake output packages.
## ```nix ## Example Usage:
## create-packages { inherit channels; src = ./my-packages; overrides = { inherit another-package; }; alias.default = "another-package"; } ## ```nix
## ``` ## create-packages { inherit channels; src = ./my-packages; overrides = { inherit another-package; }; alias.default = "another-package"; }
## Result: ## ```
## ```nix ## Result:
## { another-package = ...; my-package = ...; default = ...; } ## ```nix
## ``` ## { another-package = ...; my-package = ...; default = ...; }
#@ Attrs -> Attrs ## ```
create-packages = #@ Attrs -> Attrs
{ create-packages =
channels, {
src ? user-packages-root, channels,
pkgs ? channels.nixpkgs, src ? user-packages-root,
overrides ? { }, pkgs ? channels.nixpkgs,
alias ? { }, overrides ? { },
namespace ? snowfall-config.namespace, alias ? { },
}: namespace ? snowfall-config.namespace,
let }:
user-packages = snowfall-lib.fs.get-default-nix-files-recursive src; let
merge-packages = user-packages = snowfall-lib.fs.get-default-nix-files-recursive src;
packages: metadata: merge-packages =
packages packages: metadata:
// { packages
${metadata.name} = metadata.drv; // {
}; ${metadata.name} = metadata.drv;
packages-without-aliases = fix ( };
packages-without-aliases: packages-without-aliases = fix (
let packages-without-aliases:
create-package-metadata = let
package: create-package-metadata =
let package:
namespaced-packages = { let
${namespace} = packages-without-aliases; namespaced-packages = {
}; ${namespace} = packages-without-aliases;
extra-inputs =
pkgs
// namespaced-packages
// {
inherit channels namespace;
lib = snowfall-lib.internal.system-lib;
pkgs = pkgs // namespaced-packages;
inputs = user-inputs;
}; };
in extra-inputs =
{ pkgs
# We are building flake outputs based on file paths. Nix doesn't allow this // namespaced-packages
# so we have to explicitly discard the string's path context to use it as an attribute name. // {
name = builtins.unsafeDiscardStringContext (snowfall-lib.path.get-parent-directory package); inherit channels namespace;
drv = lib = snowfall-lib.internal.system-lib;
let pkgs = pkgs // namespaced-packages;
pkg = callPackageWith extra-inputs package { }; inputs = user-inputs;
in };
pkg in
// { {
meta = (pkg.meta or { }) // { # We are building flake outputs based on file paths. Nix doesn't allow this
snowfall = { # so we have to explicitly discard the string's path context to use it as an attribute name.
path = package; name = builtins.unsafeDiscardStringContext (snowfall-lib.path.get-parent-directory package);
drv =
let
pkg = callPackageWith extra-inputs package { };
in
pkg
// {
meta = (pkg.meta or { }) // {
snowfall = {
path = package;
};
}; };
}; };
}; };
}; packages-metadata = builtins.map create-package-metadata user-packages;
packages-metadata = builtins.map create-package-metadata user-packages; in
in foldl merge-packages { } packages-metadata
foldl merge-packages { } packages-metadata );
); packages = snowfall-lib.attrs.apply-aliases-and-overrides packages-without-aliases alias overrides;
packages = snowfall-lib.attrs.apply-aliases-and-overrides packages-without-aliases alias overrides; in
in filterPackages pkgs.stdenv.hostPlatform.system packages;
filterPackages pkgs.stdenv.hostPlatform.system packages; in
in { {
inherit create-packages; inherit create-packages;
}; };
} }

View File

@@ -11,7 +11,12 @@ let
dirOf dirOf
concatStringsSep concatStringsSep
; ;
inherit (core-inputs.nixpkgs.lib) assertMsg last init hasPrefix; inherit (core-inputs.nixpkgs.lib)
assertMsg
last
init
hasPrefix
;
file-name-regex = "(.*)\\.(.*)$"; file-name-regex = "(.*)\\.(.*)$";
in in
@@ -131,9 +136,7 @@ in
## "bar" ## "bar"
## ``` ## ```
#@ Path -> String #@ Path -> String
get-output-name = snowfall-lib.fp.compose get-output-name = snowfall-lib.fp.compose builtins.unsafeDiscardStringContext get-parent-directory;
builtins.unsafeDiscardStringContext
get-parent-directory;
## Get the directory name for flake outputs. ## Get the directory name for flake outputs.
## Example Usage: ## Example Usage:
@@ -145,9 +148,7 @@ in
## "foo" ## "foo"
## ``` ## ```
#@ Path -> String #@ Path -> String
get-directory-name = snowfall-lib.fp.compose get-directory-name = snowfall-lib.fp.compose builtins.unsafeDiscardStringContext baseNameOf;
builtins.unsafeDiscardStringContext
baseNameOf;
## Get relative module path from source directory. ## Get relative module path from source directory.
## Example Usage: ## Example Usage:

View File

@@ -16,7 +16,7 @@
## { another-shell = ...; my-shell = ...; default = ...; } ## { another-shell = ...; my-shell = ...; default = ...; }
## ``` ## ```
#@ Attrs -> Attrs #@ Attrs -> Attrs
create-shells = args: create-shells =
snowfall-lib.internal.create-simple-derivations (args // { type = "shells"; }); args: snowfall-lib.internal.create-simple-derivations (args // { type = "shells"; });
}; };
} }

View File

@@ -22,345 +22,347 @@ let
user-modules-root = snowfall-lib.fs.get-snowfall-file "modules"; user-modules-root = snowfall-lib.fs.get-snowfall-file "modules";
in in
{ {
system = let system =
## Get the name of a system based on its file path. let
## Example Usage: ## Get the name of a system based on its file path.
## ```nix ## Example Usage:
## get-inferred-system-name "/systems/my-system/default.nix" ## ```nix
## ``` ## get-inferred-system-name "/systems/my-system/default.nix"
## Result: ## ```
## ```nix ## Result:
## "my-system" ## ```nix
## ``` ## "my-system"
#@ Path -> String ## ```
get-inferred-system-name = #@ Path -> String
path: get-inferred-system-name =
if snowfall-lib.path.has-file-extension "nix" path then path:
snowfall-lib.path.get-parent-directory path if snowfall-lib.path.has-file-extension "nix" path then
else snowfall-lib.path.get-parent-directory path
baseNameOf path; else
baseNameOf path;
## Check whether a named system is macOS. ## Check whether a named system is macOS.
## Example Usage: ## Example Usage:
## ```nix ## ```nix
## is-darwin "x86_64-linux" ## is-darwin "x86_64-linux"
## ``` ## ```
## Result: ## Result:
## ```nix ## ```nix
## false ## false
## ``` ## ```
#@ String -> Bool #@ String -> Bool
is-darwin = hasInfix "darwin"; is-darwin = hasInfix "darwin";
## Check whether a named system is Linux. ## Check whether a named system is Linux.
## Example Usage: ## Example Usage:
## ```nix ## ```nix
## is-linux "x86_64-linux" ## is-linux "x86_64-linux"
## ``` ## ```
## Result: ## Result:
## ```nix ## ```nix
## false ## false
## ``` ## ```
#@ String -> Bool #@ String -> Bool
is-linux = hasInfix "linux"; is-linux = hasInfix "linux";
## Check whether a named system is virtual. ## Check whether a named system is virtual.
## Example Usage: ## Example Usage:
## ```nix ## ```nix
## is-virtual "x86_64-iso" ## is-virtual "x86_64-iso"
## ``` ## ```
## Result: ## Result:
## ```nix ## ```nix
## true ## true
## ``` ## ```
#@ String -> Bool #@ String -> Bool
is-virtual = target: (get-virtual-system-type target) != ""; is-virtual = target: (get-virtual-system-type target) != "";
## Get the virtual system type of a system target. ## Get the virtual system type of a system target.
## Example Usage: ## Example Usage:
## ```nix ## ```nix
## get-virtual-system-type "x86_64-iso" ## get-virtual-system-type "x86_64-iso"
## ``` ## ```
## Result: ## Result:
## ```nix ## ```nix
## "iso" ## "iso"
## ``` ## ```
#@ String -> String #@ String -> String
get-virtual-system-type = get-virtual-system-type =
target: target:
foldl ( foldl (
result: virtual-system: result: virtual-system:
if result == "" && hasInfix virtual-system target then virtual-system else result if result == "" && hasInfix virtual-system target then virtual-system else result
) "" virtual-systems; ) "" virtual-systems;
## Get structured data about all systems for a given target. ## Get structured data about all systems for a given target.
## Example Usage: ## Example Usage:
## ```nix ## ```nix
## get-target-systems-metadata "x86_64-linux" ## get-target-systems-metadata "x86_64-linux"
## ``` ## ```
## Result: ## Result:
## ```nix ## ```nix
## [ { target = "x86_64-linux"; name = "my-machine"; path = "/systems/x86_64-linux/my-machine"; } ] ## [ { target = "x86_64-linux"; name = "my-machine"; path = "/systems/x86_64-linux/my-machine"; } ]
## ``` ## ```
#@ String -> [Attrs] #@ String -> [Attrs]
get-target-systems-metadata = get-target-systems-metadata =
target: target:
let let
existing-systems = snowfall-lib.fs.get-directories-with-default target; existing-systems = snowfall-lib.fs.get-directories-with-default target;
create-system-metadata = path: { create-system-metadata = path: {
path = "${path}/default.nix"; path = "${path}/default.nix";
# We are building flake outputs based on file contents. Nix doesn't like this # We are building flake outputs based on file contents. Nix doesn't like this
# so we have to explicitly discard the string's path context to allow us to # so we have to explicitly discard the string's path context to allow us to
# use the name as a variable. # use the name as a variable.
name = builtins.unsafeDiscardStringContext (builtins.baseNameOf path); name = builtins.unsafeDiscardStringContext (builtins.baseNameOf path);
# We are building flake outputs based on file contents. Nix doesn't like this # We are building flake outputs based on file contents. Nix doesn't like this
# so we have to explicitly discard the string's path context to allow us to # so we have to explicitly discard the string's path context to allow us to
# use the name as a variable. # use the name as a variable.
target = builtins.unsafeDiscardStringContext (builtins.baseNameOf target); target = builtins.unsafeDiscardStringContext (builtins.baseNameOf target);
}; };
system-configurations = builtins.map create-system-metadata existing-systems; system-configurations = builtins.map create-system-metadata existing-systems;
in in
system-configurations; system-configurations;
## Get the system builder for a given target. ## Get the system builder for a given target.
## Example Usage: ## Example Usage:
## ```nix ## ```nix
## get-system-builder "x86_64-iso" ## get-system-builder "x86_64-iso"
## ``` ## ```
## Result: ## Result:
## ```nix ## ```nix
## (args: <system>) ## (args: <system>)
## ``` ## ```
#@ String -> Function #@ String -> Function
get-system-builder = get-system-builder =
target: target:
let let
virtual-system-type = get-virtual-system-type target; virtual-system-type = get-virtual-system-type target;
virtual-system-builder = virtual-system-builder =
args: args:
assert assertMsg ( assert assertMsg (
user-inputs ? nixos-generators user-inputs ? nixos-generators
) "In order to create virtual systems, you must include `nixos-generators` as a flake input."; ) "In order to create virtual systems, you must include `nixos-generators` as a flake input.";
user-inputs.nixos-generators.nixosGenerate ( user-inputs.nixos-generators.nixosGenerate (
args args
// {
format = virtual-system-type;
specialArgs = args.specialArgs // {
format = virtual-system-type;
};
modules = args.modules ++ [
../../modules/nixos/user/default.nix
];
}
);
darwin-system-builder =
args:
assert assertMsg (
user-inputs ? darwin
) "In order to create virtual systems, you must include `darwin` as a flake input.";
user-inputs.darwin.lib.darwinSystem (
(builtins.removeAttrs args [
"system"
"modules"
])
// {
specialArgs = args.specialArgs // {
format = "darwin";
};
modules = args.modules ++ [
../../modules/darwin/user/default.nix
];
}
);
linux-system-builder =
args:
core-inputs.nixpkgs.lib.nixosSystem (
args
// {
specialArgs = args.specialArgs // {
format = "linux";
};
modules = args.modules ++ [
../../modules/nixos/user/default.nix
];
}
);
in
if virtual-system-type != "" then
virtual-system-builder
else if is-darwin target then
darwin-system-builder
else
linux-system-builder;
## Get the flake output attribute for a system target.
## Example Usage:
## ```nix
## get-system-output "aarch64-darwin"
## ```
## Result:
## ```nix
## "darwinConfigurations"
## ```
#@ String -> String
get-system-output =
target:
let
virtual-system-type = get-virtual-system-type target;
in
if virtual-system-type != "" then
"${virtual-system-type}Configurations"
else if is-darwin target then
"darwinConfigurations"
else
"nixosConfigurations";
## Get the resolved (non-virtual) system target.
## Example Usage:
## ```nix
## get-resolved-system-target "x86_64-iso"
## ```
## Result:
## ```nix
## "x86_64-linux"
## ```
#@ String -> String
get-resolved-system-target =
target:
let
virtual-system-type = get-virtual-system-type target;
in
if virtual-system-type != "" then
builtins.replaceStrings [ virtual-system-type ] [ "linux" ] target
else
target;
## Create a system.
## Example Usage:
## ```nix
## create-system { path = ./systems/my-system; }
## ```
## Result:
## ```nix
## <flake-utils-plus-system-configuration>
## ```
#@ Attrs -> Attrs
create-system =
{
target ? "x86_64-linux",
system ? get-resolved-system-target target,
path,
name ? builtins.unsafeDiscardStringContext (get-inferred-system-name path),
modules ? [ ],
specialArgs ? { },
channelName ? "nixpkgs",
builder ? get-system-builder target,
output ? get-system-output target,
systems ? { },
homes ? { },
}:
let
lib = snowfall-lib.internal.system-lib;
home-system-modules = snowfall-lib.home.create-home-system-modules homes;
home-manager-module =
if is-darwin system then
user-inputs.home-manager.darwinModules.home-manager
else
user-inputs.home-manager.nixosModules.home-manager;
home-manager-modules = [ home-manager-module ] ++ home-system-modules;
in
{
inherit
channelName
system
builder
output
;
modules = [ path ] ++ modules ++ (optionals (user-inputs ? home-manager) home-manager-modules);
specialArgs = specialArgs // {
inherit
target
system
systems
lib
;
host = name;
virtual = (get-virtual-system-type target) != "";
inputs = snowfall-lib.flake.without-src user-inputs;
namespace = snowfall-config.namespace;
};
};
## Create all available systems.
## Example Usage:
## ```nix
## create-systems { hosts.my-host.specialArgs.x = true; modules.nixos = [ my-shared-module ]; }
## ```
## Result:
## ```nix
## { my-host = <flake-utils-plus-system-configuration>; }
## ```
#@ Attrs -> Attrs
create-systems =
{
systems ? { },
homes ? { },
}:
let
targets = snowfall-lib.fs.get-directories user-systems-root;
target-systems-metadata = concatMap get-target-systems-metadata targets;
user-nixos-modules = snowfall-lib.module.create-modules {
src = "${user-modules-root}/nixos";
};
user-darwin-modules = snowfall-lib.module.create-modules {
src = "${user-modules-root}/darwin";
};
nixos-modules = systems.modules.nixos or [ ];
darwin-modules = systems.modules.darwin or [ ];
create-system' =
created-systems: system-metadata:
let
overrides = systems.hosts.${system-metadata.name} or { };
user-modules = if is-darwin system-metadata.target then user-darwin-modules else user-nixos-modules;
user-modules-list = builtins.attrValues user-modules;
system-modules = if is-darwin system-metadata.target then darwin-modules else nixos-modules;
in
{
${system-metadata.name} = create-system (
overrides
// system-metadata
// { // {
systems = created-systems; format = virtual-system-type;
modules = user-modules-list ++ (overrides.modules or [ ]) ++ system-modules; specialArgs = args.specialArgs // {
inherit homes; format = virtual-system-type;
};
modules = args.modules ++ [
../../modules/nixos/user/default.nix
];
} }
); );
darwin-system-builder =
args:
assert assertMsg (
user-inputs ? darwin
) "In order to create virtual systems, you must include `darwin` as a flake input.";
user-inputs.darwin.lib.darwinSystem (
(builtins.removeAttrs args [
"system"
"modules"
])
// {
specialArgs = args.specialArgs // {
format = "darwin";
};
modules = args.modules ++ [
../../modules/darwin/user/default.nix
];
}
);
linux-system-builder =
args:
core-inputs.nixpkgs.lib.nixosSystem (
args
// {
specialArgs = args.specialArgs // {
format = "linux";
};
modules = args.modules ++ [
../../modules/nixos/user/default.nix
];
}
);
in
if virtual-system-type != "" then
virtual-system-builder
else if is-darwin target then
darwin-system-builder
else
linux-system-builder;
## Get the flake output attribute for a system target.
## Example Usage:
## ```nix
## get-system-output "aarch64-darwin"
## ```
## Result:
## ```nix
## "darwinConfigurations"
## ```
#@ String -> String
get-system-output =
target:
let
virtual-system-type = get-virtual-system-type target;
in
if virtual-system-type != "" then
"${virtual-system-type}Configurations"
else if is-darwin target then
"darwinConfigurations"
else
"nixosConfigurations";
## Get the resolved (non-virtual) system target.
## Example Usage:
## ```nix
## get-resolved-system-target "x86_64-iso"
## ```
## Result:
## ```nix
## "x86_64-linux"
## ```
#@ String -> String
get-resolved-system-target =
target:
let
virtual-system-type = get-virtual-system-type target;
in
if virtual-system-type != "" then
builtins.replaceStrings [ virtual-system-type ] [ "linux" ] target
else
target;
## Create a system.
## Example Usage:
## ```nix
## create-system { path = ./systems/my-system; }
## ```
## Result:
## ```nix
## <flake-utils-plus-system-configuration>
## ```
#@ Attrs -> Attrs
create-system =
{
target ? "x86_64-linux",
system ? get-resolved-system-target target,
path,
name ? builtins.unsafeDiscardStringContext (get-inferred-system-name path),
modules ? [ ],
specialArgs ? { },
channelName ? "nixpkgs",
builder ? get-system-builder target,
output ? get-system-output target,
systems ? { },
homes ? { },
}:
let
lib = snowfall-lib.internal.system-lib;
home-system-modules = snowfall-lib.home.create-home-system-modules homes;
home-manager-module =
if is-darwin system then
user-inputs.home-manager.darwinModules.home-manager
else
user-inputs.home-manager.nixosModules.home-manager;
home-manager-modules = [ home-manager-module ] ++ home-system-modules;
in
{
inherit
channelName
system
builder
output
;
modules = [ path ] ++ modules ++ (optionals (user-inputs ? home-manager) home-manager-modules);
specialArgs = specialArgs // {
inherit
target
system
systems
lib
;
host = name;
virtual = (get-virtual-system-type target) != "";
inputs = snowfall-lib.flake.without-src user-inputs;
namespace = snowfall-config.namespace;
}; };
created-systems = fix ( };
created-systems:
foldl ( ## Create all available systems.
systems: system-metadata: systems // (create-system' created-systems system-metadata) ## Example Usage:
) { } target-systems-metadata ## ```nix
); ## create-systems { hosts.my-host.specialArgs.x = true; modules.nixos = [ my-shared-module ]; }
in ## ```
created-systems; ## Result:
in { ## ```nix
inherit ## { my-host = <flake-utils-plus-system-configuration>; }
get-inferred-system-name ## ```
is-darwin #@ Attrs -> Attrs
is-linux create-systems =
is-virtual {
get-virtual-system-type systems ? { },
get-target-systems-metadata homes ? { },
get-system-builder }:
get-system-output let
get-resolved-system-target targets = snowfall-lib.fs.get-directories user-systems-root;
create-system target-systems-metadata = concatMap get-target-systems-metadata targets;
create-systems user-nixos-modules = snowfall-lib.module.create-modules {
; src = "${user-modules-root}/nixos";
}; };
user-darwin-modules = snowfall-lib.module.create-modules {
src = "${user-modules-root}/darwin";
};
nixos-modules = systems.modules.nixos or [ ];
darwin-modules = systems.modules.darwin or [ ];
create-system' =
created-systems: system-metadata:
let
overrides = systems.hosts.${system-metadata.name} or { };
user-modules = if is-darwin system-metadata.target then user-darwin-modules else user-nixos-modules;
user-modules-list = builtins.attrValues user-modules;
system-modules = if is-darwin system-metadata.target then darwin-modules else nixos-modules;
in
{
${system-metadata.name} = create-system (
overrides
// system-metadata
// {
systems = created-systems;
modules = user-modules-list ++ (overrides.modules or [ ]) ++ system-modules;
inherit homes;
}
);
};
created-systems = fix (
created-systems:
foldl (
systems: system-metadata: systems // (create-system' created-systems system-metadata)
) { } target-systems-metadata
);
in
created-systems;
in
{
inherit
get-inferred-system-name
is-darwin
is-linux
is-virtual
get-virtual-system-type
get-target-systems-metadata
get-system-builder
get-system-output
get-resolved-system-target
create-system
create-systems
;
};
} }

View File

@@ -56,7 +56,8 @@ in
unused-overrides = builtins.removeAttrs overrides ( unused-overrides = builtins.removeAttrs overrides (
builtins.map (metadata: metadata.name) templates-metadata builtins.map (metadata: metadata.name) templates-metadata
); );
templates = snowfall-lib.attrs.merge-with-aliases merge-templates templates-metadata alias // unused-overrides; templates =
snowfall-lib.attrs.merge-with-aliases merge-templates templates-metadata alias // unused-overrides;
in in
templates; templates;
}; };