templates/flake.nix

46 lines
1.6 KiB
Nix

{
description = "Hello World flake templates for various languages and build systems";
outputs = { self, ... }:
with builtins; let
concatMapAttrs = func: attr:
foldl' (x: y: x // y) {} (map func attr);
# Template Aliases
# Used to pick default template for each language
aliases = {
bash = "bash_script_nixpkgs";
fish = "fish_script_nixpkgs";
python = "python_pyproject_nixpkgs_basic";
};
# Get all subdirectories of the given path
# Returns [str]
subdirs = dir:
let subd = readDir dir;
in filter (d: subd.${d} == "directory") (attrNames subd);
# Get all proper recursive subdirectories with flakes in them
# Returns [[str]] (list of subdirectory sequences, for processing)
flakeSubdirs = dir:
concatMap (subd:
if pathExists /${dir}/${subd}/flake.nix then [ [ subd ] ] else
map (l: [ subd ] ++ l) (flakeSubdirs /${dir}/${subd}))
(subdirs dir);
generateDesc = subds:
"${head subds} template - ${concatStringsSep ", " (tail subds)}";
in {
templates = concatMapAttrs (subds: {
${concatStringsSep "_" subds} = {
description = generateDesc subds;
path = filterSource
(p: _: baseNameOf p != "flake.lock")
(foldl' (dir: subd: /${dir}/${subd}) ./. subds);
};
}) (flakeSubdirs ./.) // mapAttrs (from: to:
self.templates.${to} // {
description = generateDesc (filter (p: p != []) (split "_" to));
}) aliases;
};
}