diff --git a/2024/flake.nix b/2024/flake.nix index 03eb7e5..37ca20b 100644 --- a/2024/flake.nix +++ b/2024/flake.nix @@ -66,7 +66,10 @@ # }; # digression-linear-algebra = alexandriaLib.callTypstProject (import ./documents/by-name/digression-linear-algebra/package.nix); # }; - legacyPackages = alexandriaLib.parsePkgs ./documents; + legacyPackages = ( + (alexandriaLib.indexByName ./documents/by-name) + // (alexandriaLib.indexByCourse ./documents/by-course) + ); # apps = rec { # default = watch; # build = flake-utils.lib.mkApp { diff --git a/2024/nix/lib/default.nix b/2024/nix/lib/default.nix index fdecf1d..cbbe898 100644 --- a/2024/nix/lib/default.nix +++ b/2024/nix/lib/default.nix @@ -16,5 +16,8 @@ rec { inherit cleanTypstSource; } ); - parsePkgs = (import ./parsePkgs.nix) (defaultArgs // { inherit callTypstProject; }); + indexByName = (import ./indexByName.nix) (defaultArgs // { inherit callTypstProject; }); + indexByCourse = (import ./indexByCourse.nix) ( + defaultArgs // { inherit callTypstProject indexByName; } + ); } diff --git a/2024/nix/lib/indexByCourse.nix b/2024/nix/lib/indexByCourse.nix new file mode 100644 index 0000000..48541c7 --- /dev/null +++ b/2024/nix/lib/indexByCourse.nix @@ -0,0 +1,42 @@ +{ + pkgs, + callTypstProject, + indexByName, + ... +}: +baseDirectory: +let + inherit (pkgs) lib; + inherit (lib.attrsets) + mapAttrs + mapAttrsToList + mergeAttrsList + ; + inherit (builtins) readDir; +in +let + # Package files for a single shard + # Type: String -> String -> AttrsOf Path + namesForShard = + shard: type: + if type != "directory" then + # Ignore all non-directories. Technically only README.md is allowed as a file in the base directory, so we could alternatively: + # - Assume that README.md is the only file and change the condition to `shard == "README.md"` for a minor performance improvement. + # This would however cause very poor error messages if there's other files. + # - Ensure that README.md is the only file, throwing a better error message if that's not the case. + # However this would make for a poor code architecture, because one type of error would have to be duplicated in the validity checks and here. + # Additionally in either of those alternatives, we would have to duplicate the hardcoding of "README.md" + { } + else + # mapAttrs (name: _: baseDirectory + "/${shard}/${name}/package.nix") ( + # readDir (baseDirectory + "/${shard}") + # ); + mapAttrs (a: b: indexByName (baseDirectory + "/${a}")) (readDir baseDirectory); + +in +# mapAttrs (a: b: builtins.trace (builtins.tryEval a) b) ( +# mergeAttrsList (mapAttrsToList namesForShard (readDir baseDirectory)) +# ) +mapAttrs (a: b: (callTypstProject (import (/. + b)))) ( + mergeAttrsList (mapAttrsToList namesForShard (readDir baseDirectory)) +) diff --git a/2024/nix/lib/parsePkgs.nix b/2024/nix/lib/indexByName.nix similarity index 84% rename from 2024/nix/lib/parsePkgs.nix rename to 2024/nix/lib/indexByName.nix index d31776c..f596253 100644 --- a/2024/nix/lib/parsePkgs.nix +++ b/2024/nix/lib/indexByName.nix @@ -13,7 +13,7 @@ let # Package files for a single shard # Type: String -> String -> AttrsOf Path namesForShard = - shard: type: + name: type: if type != "directory" then # Ignore all non-directories. Technically only README.md is allowed as a file in the base directory, so we could alternatively: # - Assume that README.md is the only file and change the condition to `shard == "README.md"` for a minor performance improvement. @@ -23,9 +23,10 @@ let # Additionally in either of those alternatives, we would have to duplicate the hardcoding of "README.md" { } else - mapAttrs (name: _: baseDirectory + "/${shard}/${name}/package.nix") ( - readDir (baseDirectory + "/${shard}") - ); + # mapAttrs (name: _: baseDirectory + "/${shard}/${name}/package.nix") ( + # readDir (baseDirectory + "/${shard}") + # ); + mapAttrs (name: _: baseDirectory + "/${name}/package.nix") (readDir baseDirectory); in # mapAttrs (a: b: builtins.trace (builtins.tryEval a) b) (