|
| 1 | +{ |
| 2 | + description = "Flake inputs with the full power of Nixlang"; |
| 3 | + |
| 4 | + inputs.systems.url = "github:nix-systems/default"; |
| 5 | + |
| 6 | + outputs = { self, systems }: with builtins; with self.lib; { |
| 7 | + |
| 8 | + templates.default = { |
| 9 | + description = "Default template"; |
| 10 | + path = ./template; |
| 11 | + }; |
| 12 | + |
| 13 | + __functor = _: path: let s = import systems; in { systems ? s, inputFile ? "flake.in.nix", apps ? {}, ... }@outputAttrs: |
| 14 | + { |
| 15 | + nextFlake = flake path inputFile; |
| 16 | + nextFlakeSource = flakeSource path inputFile; |
| 17 | + } |
| 18 | + // outputAttrs |
| 19 | + // { |
| 20 | + apps = self.lib.genAttrs (system: |
| 21 | + { genflake = { type = "app"; program = ./genflake; }; } |
| 22 | + // apps.${system} or {} |
| 23 | + ) systems; |
| 24 | + }; |
| 25 | + |
| 26 | + lib = { |
| 27 | + |
| 28 | + flake = path: inputFile: toFile "flake.nix" (flakeSource path inputFile); |
| 29 | + |
| 30 | + flakeSource = path: inputFile: |
| 31 | + let |
| 32 | + attrs = import (path + "/${inputFile}"); |
| 33 | + attrs' = attrs // { |
| 34 | + inputs = { flakegen.url = "github:jorsn/flakegen"; } // (attrs.inputs or {}); |
| 35 | + outputs = "<outputs>"; |
| 36 | + }; |
| 37 | + in "# Do not modify! This file is generated.\n\n" |
| 38 | + + replaceStrings |
| 39 | + [ "\"<outputs>\"" ] [ "inputs: inputs.flakegen ./. ((import ./${inputFile}).outputs inputs)" ] |
| 40 | + (toPretty "" attrs') |
| 41 | + ; |
| 42 | + |
| 43 | + genAttrs = f: names: |
| 44 | + listToAttrs (map (name: { inherit name; value = f name; }) names); |
| 45 | + |
| 46 | + toPretty = |
| 47 | + let |
| 48 | + printChild = prefix: x: |
| 49 | + let |
| 50 | + names = attrNames x; |
| 51 | + in |
| 52 | + if isAttrs x && length names == 1 |
| 53 | + then "." + head names + printChild prefix x.${head names} |
| 54 | + else " = " + print prefix x |
| 55 | + ; |
| 56 | + |
| 57 | + mapAttrsToList = f: attrs: attrValues (mapAttrs f attrs); |
| 58 | + mapAttrsToLines = f: attrs: concatStringsSep "\n" (mapAttrsToList f attrs); |
| 59 | + print = prefix: x: |
| 60 | + if isString x |
| 61 | + then "\"${x}\"" |
| 62 | + else if ! isAttrs x |
| 63 | + then toString x |
| 64 | + else let prefix' = prefix + " "; in '' |
| 65 | + { |
| 66 | + ${mapAttrsToLines (n: v: prefix' + n + printChild prefix' v + ";") x} |
| 67 | + ${prefix}}''; |
| 68 | + in print; |
| 69 | + }; |
| 70 | + |
| 71 | + }; |
| 72 | + |
| 73 | +} |
0 commit comments