feat: add path.get-relative-module-path helper to extract module paths consistently

This commit is contained in:
anntnzrb
2025-10-08 23:40:44 -05:00
parent 903e38d999
commit 9969d5d8e3
2 changed files with 24 additions and 14 deletions

View File

@@ -9,7 +9,6 @@ let
inherit (core-inputs.nixpkgs.lib) inherit (core-inputs.nixpkgs.lib)
foldl foldl
mapAttrs mapAttrs
hasPrefix
hasSuffix hasSuffix
isFunction isFunction
splitString splitString
@@ -39,18 +38,7 @@ in
let let
user-modules = snowfall-lib.fs.get-default-nix-files-recursive src; user-modules = snowfall-lib.fs.get-default-nix-files-recursive src;
create-module-metadata = module: { create-module-metadata = module: {
name = name = snowfall-lib.path.get-relative-module-path src module;
let
# We are building flake outputs based on file paths. Nix doesn't allow this
# so we have to explicitly discard the string's path context for string manipulation.
path-name = builtins.replaceStrings [ (builtins.toString src) "/default.nix" ] [ "" "" ] (
builtins.unsafeDiscardStringContext module
);
in
if hasPrefix "/" path-name then
builtins.substring 1 ((builtins.stringLength path-name) - 1) path-name
else
path-name;
path = module; path = module;
}; };
modules-metadata = builtins.map create-module-metadata user-modules; modules-metadata = builtins.map create-module-metadata user-modules;

View File

@@ -11,7 +11,7 @@ let
dirOf dirOf
concatStringsSep concatStringsSep
; ;
inherit (core-inputs.nixpkgs.lib) assertMsg last init; inherit (core-inputs.nixpkgs.lib) assertMsg last init hasPrefix;
file-name-regex = "(.*)\\.(.*)$"; file-name-regex = "(.*)\\.(.*)$";
in in
@@ -146,5 +146,27 @@ in
#@ Path -> String #@ Path -> String
get-directory-name = path: get-directory-name = path:
builtins.unsafeDiscardStringContext (baseNameOf path); builtins.unsafeDiscardStringContext (baseNameOf path);
## Get relative module path from source directory.
## Example Usage:
## ```nix
## get-relative-module-path "/modules/nixos" "/modules/nixos/foo/bar/default.nix"
## ```
## Result:
## ```nix
## "foo/bar"
## ```
#@ String -> Path -> String
get-relative-module-path =
src: module:
let
path-name = builtins.replaceStrings [ (builtins.toString src) "/default.nix" ] [ "" "" ] (
builtins.unsafeDiscardStringContext module
);
in
if hasPrefix "/" path-name then
builtins.substring 1 ((builtins.stringLength path-name) - 1) path-name
else
path-name;
}; };
} }