├── flake.nix └── flake-module.nix /flake.nix: -------------------------------------------------------------------------------- 1 | { 2 | outputs = {...}: { 3 | flakeModule = ./flake-module.nix; 4 | }; 5 | } 6 | -------------------------------------------------------------------------------- /flake-module.nix: -------------------------------------------------------------------------------- 1 | { 2 | lib, 3 | flake-parts-lib, 4 | inputs, 5 | ... 6 | }: 7 | let 8 | inherit (flake-parts-lib) 9 | mkPerSystemOption 10 | ; 11 | inherit (lib) 12 | mkOption 13 | types 14 | ; 15 | in 16 | { 17 | options = { 18 | perSystem = mkPerSystemOption ( 19 | { ... }: 20 | { 21 | options = { 22 | pkgsDirectory = mkOption { 23 | type = types.nullOr types.path; 24 | default = null; 25 | description = '' 26 | If set, the flake will import packages from the specified directory. 27 | ''; 28 | }; 29 | 30 | pkgsNameSeparator = mkOption { 31 | type = types.str; 32 | default = "/"; 33 | description = '' 34 | The separator to use when flattening package names. 35 | ''; 36 | }; 37 | }; 38 | } 39 | ); 40 | }; 41 | 42 | config = { 43 | perSystem = 44 | { config, pkgs, ... }: 45 | let 46 | flattenPkgs = 47 | separator: path: value: 48 | if lib.isDerivation value then 49 | { 50 | ${lib.concatStringsSep separator path} = value; 51 | } 52 | else if lib.isAttrs value then 53 | lib.concatMapAttrs (name: flattenPkgs separator (path ++ [ name ])) value 54 | else 55 | # Ignore the functions which makeScope returns 56 | { }; 57 | 58 | inputsScope = lib.makeScope pkgs.newScope (self: { 59 | inherit inputs; 60 | }); 61 | 62 | scopeFromDirectory = 63 | directory: 64 | lib.filesystem.packagesFromDirectoryRecursive { 65 | inherit directory; 66 | inherit (inputsScope) newScope callPackage; 67 | }; 68 | 69 | legacyPackages = scopeFromDirectory config.pkgsDirectory; 70 | in 71 | lib.mkIf (config.pkgsDirectory != null) { 72 | inherit legacyPackages; 73 | packages = flattenPkgs config.pkgsNameSeparator [ ] legacyPackages; 74 | }; 75 | }; 76 | } 77 | --------------------------------------------------------------------------------