Generate flake.nix from module options. Discussions: https://oeiuwq.zulipchat.com/join/nqp26cd4kngon6mo3ncgnuap/
dendrix.oeiuwq.com/Dendritic.html
dendritic
nix
inputs
1{ lib, config, ... }@top:
2let
3 inherit (config) flake-file;
4
5 inherit (import ./../dev/modules/_lib lib) inputsExpr nixCode;
6
7 inputsString = ''
8 # DO-NOT-EDIT: Generated by github:vic/flake-file.
9 # To re-generate use: nix-shell . -A flake-file.sh --run write-inputs
10 ''
11 + (nixCode { expr = inputsExpr flake-file.inputs; });
12
13 inputsFile =
14 pkgs:
15 pkgs.stdenvNoCC.mkDerivation {
16 name = "inputs";
17 passAsFile = [ "inputsString" ];
18 inherit inputsString;
19 phases = [ "copy" ];
20 copy = ''
21 cp $inputsStringPath $out
22 '';
23 };
24
25 write-inputs =
26 pkgs:
27 pkgs.writeShellApplication {
28 name = "write-inputs";
29 text = ''
30 cd ${flake-file.intoPath}
31 cat ${inputsFile pkgs} > "''${1:-inputs.nix}"
32 ${lib.getExe pkgs.nixfmt} "''${1:-inputs.nix}"
33 '';
34 };
35
36 shell =
37 pkgs:
38 pkgs.mkShell {
39 buildInputs = map (f: f pkgs) (lib.attrValues flake-file.apps);
40 };
41in
42{
43 config.flake-file.apps = { inherit write-inputs; };
44
45 options.flake-file = {
46 pkgs = lib.mkOption {
47 type = lib.types.raw;
48 description = "nixpkgs instance for unflake generator";
49 default = import (top.inputs.nixpkgs or <nixpkgs>) { };
50 };
51
52 apps = lib.mkOption {
53 type = lib.types.lazyAttrsOf (lib.types.functionTo lib.types.package);
54 default = { };
55 description = "Attrs of (pkgs -> app) to include in shell";
56 };
57
58 intoPath = lib.mkOption {
59 internal = true;
60 visible = false;
61 type = lib.types.str;
62 default = ".";
63 };
64
65 inputsFile = lib.mkOption {
66 type = lib.types.raw;
67 readOnly = true;
68 internal = true;
69 visible = false;
70 default = inputsFile;
71 };
72
73 sh = lib.mkOption {
74 type = lib.types.raw;
75 readOnly = true;
76 internal = true;
77 visible = false;
78 default = shell flake-file.pkgs;
79 };
80 };
81}