commit 56f6b2906cd58eaba58e38a711afce0ab3c25c47
parent 5f218da7286f30123d1ce20a70d1de633e803427
Author: Katja Ramona Sophie Kwast (zaphyra) <git@zaphyra.eu>
Date: Tue, 19 Aug 2025 13:27:24 +0200
parent 5f218da7286f30123d1ce20a70d1de633e803427
Author: Katja Ramona Sophie Kwast (zaphyra) <git@zaphyra.eu>
Date: Tue, 19 Aug 2025 13:27:24 +0200
flake: add `nixosModules`
1 file changed, 57 insertions(+), 2 deletions(-)
diff --git a/flake.nix b/flake.nix @@ -16,6 +16,55 @@ let maintainersAttrset = import ./maintainers.nix; nixpkgsLib = inputs.nixpkgs.lib; + toCamelCase = + str: + nixpkgsLib.throwIfNot (nixpkgsLib.isString str) "toCamelCase does only accepts string values, but got ${nixpkgsLib.typeOf str}" ( + let + separators = nixpkgsLib.splitStringBy ( + prev: curr: + builtins.elem curr [ + "-" + "_" + " " + ] + ) false str; + + parts = nixpkgsLib.flatten ( + map (nixpkgsLib.splitStringBy ( + prev: curr: nixpkgsLib.match "[a-z]" prev != null && nixpkgsLib.match "[A-Z]" curr != null + ) true) separators + ); + + first = if nixpkgsLib.length parts > 0 then nixpkgsLib.toLower (nixpkgsLib.head parts) else ""; + rest = if nixpkgsLib.length parts > 1 then builtins.map nixpkgsLib.toSentenceCase (nixpkgsLib.tail parts) else [ ]; + in + nixpkgsLib.concatStrings (map (nixpkgsLib.addContextFrom str) ([ first ] ++ rest)) + ); + collectModules = modulesDir: ( + let + files = nixpkgsLib.pipe (nixpkgsLib.fileset.fileFilter (file: file.name == "default.nix") modulesDir) [ + nixpkgsLib.fileset.toList + (builtins.map builtins.toString) + ]; + in nixpkgsLib.pipe files [ + (builtins.map (elem: + let + pathList = nixpkgsLib.pipe (nixpkgsLib.splitString "/" elem) [ + (nixpkgsLib.drop 4) + ]; + name = nixpkgsLib.pipe pathList [ + (nixpkgsLib.drop 1) + (nixpkgsLib.dropEnd 1) + (builtins.concatStringsSep "-") + toCamelCase + ]; + path = ./. + (builtins.concatStringsSep "/" ([ "" ] ++ pathList)); + in nixpkgsLib.nameValuePair name (import path) + )) + builtins.listToAttrs + ] + ); + forAllSystems = function: nixpkgsLib.genAttrs @@ -52,10 +101,16 @@ }; }; - nixosModules = { - default = { + nixosModules = let + modules = collectModules ./nixosModules; + in modules // { + allModules = { ... }: { + imports = builtins.attrValues modules; + }; + allPackages = { ... }: { nixpkgs.overlays = [ inputs.self.overlays.default ]; }; + default = inputs.self.nixosModules.allPackages; }; }