diff --git a/snowfall-lib/module/default.nix b/snowfall-lib/module/default.nix index 27f38f3..3998463 100644 --- a/snowfall-lib/module/default.nix +++ b/snowfall-lib/module/default.nix @@ -9,7 +9,6 @@ let inherit (core-inputs.nixpkgs.lib) foldl mapAttrs - hasPrefix hasSuffix isFunction splitString @@ -39,18 +38,7 @@ in let user-modules = snowfall-lib.fs.get-default-nix-files-recursive src; create-module-metadata = module: { - name = - 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; + name = snowfall-lib.path.get-relative-module-path src module; path = module; }; modules-metadata = builtins.map create-module-metadata user-modules; diff --git a/snowfall-lib/path/default.nix b/snowfall-lib/path/default.nix index 894b5b2..a5c7bbc 100644 --- a/snowfall-lib/path/default.nix +++ b/snowfall-lib/path/default.nix @@ -11,7 +11,7 @@ let dirOf concatStringsSep ; - inherit (core-inputs.nixpkgs.lib) assertMsg last init; + inherit (core-inputs.nixpkgs.lib) assertMsg last init hasPrefix; file-name-regex = "(.*)\\.(.*)$"; in @@ -146,5 +146,27 @@ in #@ Path -> String get-directory-name = 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; }; }