···5566import { Aside } from '@astrojs/starlight/components';
7788-99-Suppose you have a flake that looks like:
1010-1111-```nix
1212-# flake.nix
1313-{
1414- inputs = { # So much inputs
1515- nixpkgs.url = "github:nixos/nixpkgs/unstable";
1616- foo.url = "github:coolguy/foo";
1717- bar.url = "github:coolguy/bar";
1818- };
1919- outputs = inputs: { ... }; # wow! very outputs
2020-}
2121-```
2222-2323-## Boostraping flake-file
2424-2525-First, move your `flake.nix` file as `flake-file.nix`.
88+## The bootstrap magic step.
2692710```shell
2811mv flake.nix flake-file.nix
1212+nix-shell https://github.com/vic/flake-file/archive/main.tar.gz -A flake-file.sh --run bootstrap
2913```
30143131-And now, the bootstrapping magic step:
1515+## The new `flake.nix` (static Nix)
32163333-```shell
3434-nix-shell https://github.com/vic/flake-file/archive/main.tar.gz \
3535- -A flake-file.sh --run write-flake \
3636- --arg modules ./flake-file.nix --argstr outputs flake-file
3737-```
1717+You will notice your **new** `flake.nix` has your same meta-data: `inputs`, `description`, `nixConfig`. Only the `outputs` function was changed.
38183939-> <small>See also: all [bootstrap command args](/reference/bootstrap)</small>
1919+<img src="https://i.kym-cdn.com/photos/images/newsfeed/001/449/977/aba.gif" />
40202121+And the `outputs` function treats your `flake-file.nix` - your original flake.nix file -
2222+as an ordinary Nix module. No changes required on your existing Nix logic.
41234242-## The new `flake.nix` (static Nix)
2424+## Your `flake-file.nix` (non-static **real** Nix)
43254444-You will notice your new `flake.nix` looks like this:
2626+Your inputs and all other flake meta-data are now module options (`flake-file.inputs`, etc).
45274646-```diff lang="nix" "(A)" "(B)" "(C)" "(D)"
4747- # flake.nix
4848- # DO-NOT-EDIT. This file was auto-generated using github:vic/flake-file. (A)
4949- # Use `nix run .#write-flake` to regenerate it. (D)
5050- {
5151- outputs = inputs: (import ./flake-file.nix).outputs inputs; # (B)
5252- inputs = {
5353- # (C) all your inputs (static version)
5454- };
5555- }
5656-```
5757-<strong>(A)</strong> The `--arg modules ./flake-file.nix` option indicates
5858-the module entry-point. Your old flake is **already** a module for flake-file.
2828+`flake.nix` is a generated static asset intended for code distribution.
59296060-<strong>(B)</strong> The `--argstr outputs flake-file` generated
6161-an `outputs` function that just **delegates** to the one in `flake-file.nix`.
3030+## Output Schemas
62316363-<strong>(C)</strong> Your `inputs` are now the **static** version (Nix subset)
6464-of the **real Nix** inputs at your `flake-file.nix` file.
3232+Your outputs are now freeform types but can also be validated by **output schemas**.
3333+3434+And they work with **any** Nix implementation: Lix, CppNix, DetSys, etc.
65356666-## Edit `flake-file.nix` (real Nix)
3636+Add the following `flake-file.outputs-schema` to your `flake-file.nix` and then
3737+run `nix flake check`, then try replacing `mundo` by `world`.
67386868-Now that `flake-file.nix` is real Nix code, you can refactor your inputs code
6969-to your heart's content.
3939+No extra check-derivation was needed, no custom nix-implementation,
4040+the checks are by the Nix module system as normal.
70417142```nix
7243# flake-file.nix
7373-# Now with more and **real** Nix
7474-let
7575- # for example purposes
7676- gh = user: repo: branch: "github:${user}/${repo}/${branch}";
4444+{
4545+ outputs = inputs: { hello = "mundo"; };
77467878- coolguy = repo: gh "coolguy" repo "main";
7979- channel = "unstable";
8080-in {
8181- inputs = {
8282- nixpkgs.url = gh "nixos" "nixpkgs" channel;
8383- foo.url = coolguy "foo";
8484- bar.url = coolguy "bar";
4747+ flake-file.outputs-schema = { lib, ... }: {
4848+ options.hello = lib.mkOption {
4949+ type = lib.types.enum [ "mundo" ];
5050+ };
8551 };
8686-8787- outputs = inputs: { ... }; # keeps the same
8852}
8953```
90549191-## Update static from dynamic Nix.
5555+## Update `flake.nix`
92569393-Run the **same** command again, your input changes will be reflected into
5757+You can run the **bootstrap** command whenever you want.
5858+5959+When your input changes, run it again to propagate your input changes into
9460the **static** `flake.nix`.
95619662```shell
9797-nix-shell https://github.com/vic/flake-file/archive/main.tar.gz \
9898- -A flake-file.sh --run write-flake \
9999- --arg modules ./flake-file.nix --argstr outputs flake-file
6363+nix-shell https://github.com/vic/flake-file/archive/main.zip -A flake-file.sh --run bootstrap
10064```
1016510266<Aside title="You are done!">
10367You can stop here and enjoy your dynamic inputs at `flake-file.nix`.
1046810569Simply run the same bootstrap command whenever inputs change.
106106-</Aside>
10770108108-## Moving into a module-based flake.
109109-110110-<strong>(D)</strong> You might have noticed the comment about the `.#write-flake` app.
111111-To include this app as part of your flake and not having use the bootstrap command
112112-and specify all the same options each time, you need:
113113-114114-Use `flake-module` outputs instead of `flake-file` on the bootstrap command:
115115-116116-```shell ins="flake-module"
117117-nix-shell https://github.com/vic/flake-file/archive/main.tar.gz \
118118- -A flake-file.sh --run write-flake \
119119- --arg modules ./flake-file.nix --argstr outputs flake-module
120120-```
121121-122122-Add options to your `flake-file.nix` **module**. I told you it was a module already.
123123-124124-```diff lang="nix" "resolved flake inputs" " inputs =" "flake-file.inputs option" "inputs," " outputs =" "type-checked schema" ins="flake-module"
125125- # flake-file.nix
126126-+ { inputs, lib, ... }: # resolved flake inputs as specialArgs
127127- let
128128- # same helper code as before
129129- in
130130- {
131131- # same inputs code as before
132132- inputs = { # Alias option for flake-file.inputs type-checked schema!
133133-134134- # make sure you add flake-file dependency.
135135-+ flake-file.url = lib.mkDefault "github:vic/flake-file";
136136- };
137137-138138- # same outputs code as before
139139- outputs = inputs: ...; # Now with **extensible** type-checked schema!
140140-141141- # since this IS a module it can import other modules
142142- imports = [
143143- # enable inside-flake and say goodbye to bootstrap
144144-+ inputs.flake-file.flakeModule
145145-146146- # start splitting from huge monolithic code to modular nix.
147147- # each input can be defined on their relevant module files.
148148- # maybe move outputs function to ./outputs.nix
149149- ];
150150-151151- # generate the same output function we used at bootstrap
152152-+ flake-file.outputs = "flake-module";
153153- }
154154-```
155155-156156-157157-## Your flake is flake-file enabled
158158-159159-Commands for your daily life:
160160-161161-```shell
162162-nix run .#write-flake # whenever you need to regen flake.nix
163163-164164-nix flake check # will make sure your flake.nix is up-to-date
165165-```
7171+See Reference for integrating it as a runnable package from your flake.
7272+</Aside>