|
1 | 1 | lib: rec { |
| 2 | + /* |
| 3 | + Function: mkNixpkgs |
| 4 | + Synopsis: Creates a custom Nixpkgs configuration. |
| 5 | +
|
| 6 | + Parameters: |
| 7 | + - system (string): Target system, e.g., "x86_64-linux". |
| 8 | + - inputs (attrset, optional): Custom inputs for the Nixpkgs configuration. |
| 9 | + - overlays (list, optional): List of overlays to apply. |
| 10 | + - nixpkgs (path, optional): Path to the Nixpkgs repository. Defaults to inputs.nixpkgs. |
| 11 | + - config (attrset, optional): Additional Nixpkgs configuration settings. |
| 12 | +
|
| 13 | + Returns: |
| 14 | + - A configured Nixpkgs environment suitable for importing. |
| 15 | +
|
| 16 | + Example: |
| 17 | + mkNixpkgs { |
| 18 | + system = "x86_64-linux"; |
| 19 | + overlays = [ myOverlay ]; |
| 20 | + } |
| 21 | +
|
| 22 | + Description: |
| 23 | + The function imports a Nixpkgs environment with the specified target system, custom inputs, |
| 24 | + and overlays. It also accepts additional Nixpkgs configuration settings. |
| 25 | + */ |
| 26 | + mkNixpkgs = { |
| 27 | + system, |
| 28 | + nixpkgs, |
| 29 | + overlays ? [], |
| 30 | + config ? {allowUnfree = true;}, |
| 31 | + }: |
| 32 | + import nixpkgs {inherit system config overlays;}; |
| 33 | + |
| 34 | + /* |
| 35 | + Function: mkApp |
| 36 | + Synopsis: Creates an "app" type for Nix flakes. |
| 37 | +
|
| 38 | + Parameters: |
| 39 | + - drv (derivation): The Nix derivation. |
| 40 | + - name (string, optional): Name of the application. |
| 41 | + - exePath (string, optional): Executable path. |
| 42 | +
|
| 43 | + Returns: |
| 44 | + - An "app" type attribute with 'type' and 'program' keys. |
| 45 | + */ |
| 46 | + mkApp = { |
| 47 | + drv, |
| 48 | + name ? drv.pname or drv.name, |
| 49 | + exePath ? drv.passthru.exePath or "/bin/${name}", |
| 50 | + }: { |
| 51 | + type = "app"; |
| 52 | + program = "${drv}${exePath}"; |
| 53 | + }; |
| 54 | + |
| 55 | + /* |
| 56 | + Function: buildApps |
| 57 | + Synopsis: Constructs attribute set of applications from Nix packages and custom apps specification. |
| 58 | +
|
| 59 | + Parameters: |
| 60 | + - packages (attrset): An attribute set of Nix packages. |
| 61 | + - apps (attrset): Custom apps specification. |
| 62 | +
|
| 63 | + Returns: |
| 64 | + - An attribute set representing built applications. |
| 65 | + */ |
| 66 | + buildApps = packages: apps: |
| 67 | + lib.listToAttrs |
| 68 | + (lib.collect (attrs: lib.attrNames attrs == ["name" "value"]) |
| 69 | + (lib.mapAttrsRecursiveCond lib.isAttrs (path: v: let |
| 70 | + drvName = lib.head path; |
| 71 | + drv = packages.${drvName}; |
| 72 | + name = lib.last (lib.init path); |
| 73 | + exePath = "/bin/${v}"; |
| 74 | + in |
| 75 | + lib.nameValuePair name {inherit drv name exePath;}) |
| 76 | + apps)); |
| 77 | + |
| 78 | + /* |
| 79 | + Function: platformPkgs |
| 80 | + Synopsis: Filters Nix packages based on the target system platform. |
| 81 | +
|
| 82 | + Parameters: |
| 83 | + - system (string): Target system platform (e.g., "x86_64-linux"). |
| 84 | +
|
| 85 | + Returns: |
| 86 | + - A filtered attribute set of Nix packages compatible with the target system. |
| 87 | + */ |
| 88 | + platformPkgs = system: |
| 89 | + lib.filterAttrs |
| 90 | + (_: value: let |
| 91 | + platforms = lib.attrByPath ["meta" "platforms"] [] value; |
| 92 | + in |
| 93 | + lib.elem system platforms); |
| 94 | + |
| 95 | + /* |
| 96 | + Function: platformApps |
| 97 | + Synopsis: Filters and builds platform-specific applications. |
| 98 | +
|
| 99 | + Parameters: |
| 100 | + - packages (attrset): An attribute set of Nix packages. |
| 101 | + - apps (attrset): Custom apps specification. |
| 102 | +
|
| 103 | + Returns: |
| 104 | + - An attribute set of platform-specific applications. |
| 105 | + */ |
| 106 | + platformApps = packages: apps: let |
| 107 | + apps' = lib.filterAttrs (name: _: lib.elem name (lib.attrNames packages)) apps; |
| 108 | + bapps = buildApps packages apps'; |
| 109 | + in |
| 110 | + lib.mapAttrs (_: mkApp) bapps; |
| 111 | + |
2 | 112 | /* |
3 | 113 | Function: flattenTree |
4 | 114 | Synopsis: Flattens a nested attribute set (tree) into a single-level attribute set. |
|
0 commit comments