Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ This means

```shell
mv flake.nix flake-file.nix
nix-shell https://github.com/vic/flake-file/archive/main.zip -A flake-file.sh --run bootstrap
nix-shell https://github.com/vic/flake-file/archive/main.zip -A bootstrap --run bootstrap
```
114 changes: 114 additions & 0 deletions dev/_bootstrap-tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ let
outputs = _: { };
};

broken-app = bootstrap {
inputs.empty.url = "github:vic/empty-flake";
outputs = _: { };
flake-file.apps.write-broken =
pkgs:
pkgs.runCommand "write-broken" { } ''
exit 1
'';
};

all-inputs-schemes = bootstrap {
inputs.simple.url = "github:vic/empty-flake";
inputs.withBranch.url = "github:vic/empty-flake/main";
Expand Down Expand Up @@ -48,6 +58,36 @@ let
inputs.flake-parts.url = "github:hercules-ci/flake-parts";
};

bootstrap-all = import ./.. (
args
// {
bootstrap = true;
modules = { outputs = _: { }; };
}
);

bootstrap-selected = import ./.. (
args
// {
bootstrap = [
"flake-file"
"nixpkgs"
];
modules = { outputs = _: { }; };
}
);

bootstrap-override = import ./.. (
args
// {
bootstrap = true;
modules = {
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
outputs = _: { };
};
}
);

flake-parts-follows = bootstrap {
inputs.nixpkgs-lib.url = "github:vic/empty-flake";
inputs.flake-parts.url = "github:hercules-ci/flake-parts";
Expand Down Expand Up @@ -84,6 +124,75 @@ let
'';
};

test-flake-public = pkgs.writeShellApplication {
name = "test-flake-public";
runtimeInputs = [
pkgs.nix
(empty.flake-file.apps.write-flake pkgs)
];
text = ''
write-flake
cat ${outdir}/flake.nix
grep github:vic/empty-flake ${outdir}/flake.nix
'';
};

test-flake-public-ignores-broken-app = pkgs.writeShellApplication {
name = "test-flake-public-ignores-broken-app";
runtimeInputs = [
pkgs.nix
(broken-app.flake-file.apps.write-flake pkgs)
];
text = ''
write-flake
cat ${outdir}/flake.nix
grep github:vic/empty-flake ${outdir}/flake.nix
'';
};

test-bootstrap-all-inputs = pkgs.writeShellApplication {
name = "test-bootstrap-all-inputs";
runtimeInputs = [
(bootstrap-all.flake-file.apps.write-inputs pkgs)
];
text = ''
write-inputs
grep github:vic/import-tree ${outdir}/inputs.nix
grep github:vic/flake-file ${outdir}/inputs.nix
grep github:hercules-ci/flake-parts ${outdir}/inputs.nix
grep nixpkgs-unstable ${outdir}/inputs.nix
'';
};

test-bootstrap-selected-inputs = pkgs.writeShellApplication {
name = "test-bootstrap-selected-inputs";
runtimeInputs = [
(bootstrap-selected.flake-file.apps.write-inputs pkgs)
];
text = ''
write-inputs
grep github:vic/flake-file ${outdir}/inputs.nix
grep nixpkgs-unstable ${outdir}/inputs.nix
if grep github:vic/import-tree ${outdir}/inputs.nix; then
exit 1
fi
if grep github:hercules-ci/flake-parts ${outdir}/inputs.nix; then
exit 1
fi
'';
};

test-bootstrap-input-override = pkgs.writeShellApplication {
name = "test-bootstrap-input-override";
runtimeInputs = [
(bootstrap-override.flake-file.apps.write-inputs pkgs)
];
text = ''
write-inputs
grep github:nixos/nixpkgs/nixos-unstable ${outdir}/inputs.nix
'';
};

test-npins = pkgs.writeShellApplication {
name = "test-npins";
runtimeInputs = [
Expand Down Expand Up @@ -246,6 +355,11 @@ pkgs.mkShell {
buildInputs = [
test-inputs
test-flake
test-flake-public
test-flake-public-ignores-broken-app
test-bootstrap-all-inputs
test-bootstrap-selected-inputs
test-bootstrap-input-override
test-unflake
test-npins
test-npins-schemes
Expand Down
2 changes: 1 addition & 1 deletion docs/src/content/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Ever wanted to interpolate a string just to discover inputs are **NOT REAL** Nix

```shell
mv flake.nix flake-file.nix
nix-shell https://github.com/vic/flake-file/archive/main.tar.gz -A flake-file.sh --run bootstrap
nix-shell https://github.com/vic/flake-file/archive/main.tar.gz -A bootstrap --run bootstrap
```

### Features
Expand Down
11 changes: 8 additions & 3 deletions docs/src/content/docs/reference/bootstrap.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ The bootstrap command lets you generate `flake.nix`, `unflake.nix`, `.tack/`, or

```shell
nix-shell https://github.com/vic/flake-file/archive/refs/heads/main.zip \
-A flake-file.sh --run <app> [--arg <name> <value>]...
-A <app> --run <app> [--arg <name> <value>]...
```

## Apps

The `flake-file.sh` shell provides these commands:
Each app is available as a standalone top-level target. Use the matching
`-A <app>` target when running one command so unrelated backends are not
realized. Use `-A flake-file.sh` only when you want an aggregate shell with all
commands.

The bootstrap entrypoint provides these commands:

| App | Description |
| --- | --- |
Expand Down Expand Up @@ -46,7 +51,7 @@ The Nix file or directory containing your flake-file module definitions. All `.n

### `bootstrap`

Include predefined inputs from the built-in `bootstrap.nix`. When set to `true`, all bootstrap inputs are included. When set to a list, only the named inputs are included.
Include predefined inputs from `modules/bootstrap/inputs.nix`. When set to `true`, all bootstrap inputs are included. When set to a list, only the named inputs are included. These defaults are imported before your modules, so your modules can override them.

- **Type:** `true` | list of input names
- **Default:** `[]` (none)
Expand Down
6 changes: 3 additions & 3 deletions docs/src/content/docs/tutorials/bootstrap.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ echo '{ inputs.flake-file.url = "github:vic/flake-file"; }' > flake-file.nix

# Generate flake.nix or unflake.nix or npins from flake-file.nix
nix-shell https://github.com/vic/flake-file/archive/refs/heads/main.zip \
-A flake-file.sh --run write-flake --arg modules ./flake-file.nix
-A write-flake --run write-flake --arg modules ./flake-file.nix
```

> <small>See also: all [bootstrap command args](/reference/bootstrap)</small>

Replace `write-flake` with `write-inputs`, `write-unflake`, `write-tack`, or `write-npins` to target a different backend.
Replace `write-flake` in both `-A write-flake` and `--run write-flake` with `write-inputs`, `write-unflake`, `write-tack`, or `write-npins` to target a different backend.


## Using a modules directory
Expand All @@ -37,7 +37,7 @@ Replace `write-flake` with `write-inputs`, `write-unflake`, `write-tack`, or `wr

```shell "./modules"
nix-shell https://github.com/vic/flake-file/archive/refs/heads/main.zip \
-A flake-file.sh --run write-flake --arg modules ./modules
-A write-flake --run write-flake --arg modules ./modules
```

This is useful when you already have module files and just need the initial `flake.nix` to be generated before Nix can evaluate the flake itself.
Expand Down
2 changes: 1 addition & 1 deletion docs/src/content/docs/tutorials/migrate-flake-parts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ And now, lets replace `flake.nix` using bootstrapping magic step:

```shell "./modules" "flake-parts"
nix-shell https://github.com/vic/flake-file/archive/main.tar.gz \
-A flake-file.sh --run write-flake \
-A write-flake --run write-flake \
--arg modules ./modules --argstr outputs flake-parts
```

Expand Down
4 changes: 2 additions & 2 deletions docs/src/content/docs/tutorials/migrate-no-flakes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ And now, lets generate `./npins` from `./flake-file.nix`.

```shell "./flake-file.nix" "write-npins"
nix-shell https://github.com/vic/flake-file/archive/main.tar.gz \
-A flake-file.sh --run write-npins --arg modules ./flake-file.nix
-A write-npins --run write-npins --arg modules ./flake-file.nix
```

> <small>See also: all [bootstrap command args](/reference/bootstrap)</small>
Expand Down Expand Up @@ -118,7 +118,7 @@ To update npins from inputs at ./modules do:

```shell "./modules" "write-npins"
nix-shell https://github.com/vic/flake-file/archive/main.tar.gz \
-A flake-file.sh --run write-npins --arg modules ./modules
-A write-npins --run write-npins --arg modules ./modules
```

### `(A)` flake-like inputs
Expand Down
4 changes: 2 additions & 2 deletions docs/src/content/docs/tutorials/migrate-traditional-flake.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ You can use [`templates/minimal`](https://github.com/vic/flake-file/tree/main/te

```shell
mv flake.nix flake-file.nix
nix-shell https://github.com/vic/flake-file/archive/main.tar.gz -A flake-file.sh --run bootstrap
nix-shell https://github.com/vic/flake-file/archive/main.tar.gz -A bootstrap --run bootstrap
```

## The new `flake.nix` (static Nix)
Expand Down Expand Up @@ -64,7 +64,7 @@ When your input changes, run it again to propagate your input changes into
the **static** `flake.nix`.

```shell
nix-shell https://github.com/vic/flake-file/archive/main.zip -A flake-file.sh --run bootstrap
nix-shell https://github.com/vic/flake-file/archive/main.zip -A bootstrap --run bootstrap
```

<Aside title="You are done!">
Expand Down
25 changes: 22 additions & 3 deletions modules/bootstrap/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
pkgs ? import <nixpkgs> { },
modules ? [ ],
bootstrap ? [ ],
outdir ? ".",
inputs ? null,
outputs ? null,
Expand All @@ -20,13 +21,27 @@ let

tree = (import import-tree) modules;

bootstrapModule =
if bootstrap == true then
./inputs.nix
else if builtins.isList bootstrap then
let
allInputs = (import ./inputs.nix { inherit lib; }).flake-file.inputs;
in
{ flake-file.inputs = lib.filterAttrs (name: _: builtins.elem name bootstrap) allInputs; }
else if bootstrap == false then
{ }
else
throw "flake-file bootstrap must be true, false, or a list of input names";

attrsOpt = lib.mkOption {
default = { };
type = lib.types.submodule { freeformType = lib.types.lazyAttrsOf lib.types.unspecified; };
};

module = {
imports = [
bootstrapModule
tree
./../default.nix
./../options
Expand Down Expand Up @@ -60,7 +75,7 @@ let
inputs = ''{ flake-file.url = "github:vic/flake-file"; }'';
notice = pkgs.writeText "notice" ''
# DO-NOT-EDIT. Generated by github:vic/flake-file from flake-file.nix. Regen with:
# nix-shell https://github.com/vic/flake-file/archive/main.zip -A flake-file.sh --run bootstrap
# nix-shell https://github.com/vic/flake-file/archive/main.zip -A bootstrap --run bootstrap
'';
in
pkgs.writeShellApplication {
Expand All @@ -69,7 +84,7 @@ let
if ! test -f flake-file.nix; then
cp flake.nix flake-file.nix
fi
nix-shell ${../..} -A flake-file.sh --run write-flake \
nix-shell ${../..} -A write-flake --run write-flake \
--arg modules ./flake-file.nix \
--arg do-not-edit ${notice} \
--argstr outputs flake-module \
Expand All @@ -80,5 +95,9 @@ let

evaled = lib.evalModules { modules = [ module ]; };

appPackages = lib.mapAttrs (
_: app: pkgs.mkShell { buildInputs = [ (app pkgs) ]; }
) evaled.config.flake-file.apps;

in
evaled.config
evaled.config // appPackages
Loading