Skip to content

Commit f986f21

Browse files
committed
bootstrap command for traditional flakes.
1 parent f18f9ba commit f986f21

7 files changed

Lines changed: 64 additions & 23 deletions

File tree

.github/workflows/flake-check.yaml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,25 @@ jobs:
1616
templates=$(find templates -mindepth 2 -maxdepth 2 -name flake.nix -print0 | xargs -0 dirname | xargs -n 1 basename | jq -R | jq -sc)
1717
echo "$templates"
1818
echo "templates=$templates" >> $GITHUB_OUTPUT
19-
bootstrap:
20-
name: Bootstrap
19+
bootstrap-command:
20+
name: Bootstrap command
2121
runs-on: ubuntu-latest
2222
env:
23-
NIX_PATH: "nixpkgs=https://github.com/NixOS/nixpkgs/archive/nixos-unstable.tar.gz"
23+
NIX_PATH: "nixpkgs=https://channels.nixos.org/nixpkgs-unstable/nixexprs.tar.xz"
24+
steps:
25+
- uses: wimpysworld/nothing-but-nix@main
26+
- uses: cachix/install-nix-action@v31
27+
- uses: DeterminateSystems/magic-nix-cache-action@main
28+
- uses: actions/checkout@v4
29+
- run: echo '{ }' > flake.nix
30+
- run: nix-shell "https://github.com/$GITHUB_REPOSITORY/archive/$GITHUB_SHA.zip" -A flake-file.sh --run bootstrap
31+
- run: grep inputs.flake-file.flakeModules.flake flake.nix
32+
- run: grep github:vic/flake-file flake.nix
33+
bootstrap-tests:
34+
name: Bootstrap apps
35+
runs-on: ubuntu-latest
36+
env:
37+
NIX_PATH: "nixpkgs=https://channels.nixos.org/nixpkgs-unstable/nixexprs.tar.xz"
2438
steps:
2539
- uses: wimpysworld/nothing-but-nix@main
2640
- uses: cachix/install-nix-action@v31

modules/bootstrap/default.nix

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
pkgs ? import <nixpkgs> { },
33
modules ? [ ],
44
outdir ? ".",
5-
bootstrap ? [ ],
5+
inputs ? null,
66
outputs ? null,
7+
do-not-edit ? null,
78
import-tree ? (
89
pkgs.fetchFromGitHub {
910
owner = "vic";
@@ -24,15 +25,6 @@ let
2425
type = lib.types.submodule { freeformType = lib.types.lazyAttrsOf lib.types.unspecified; };
2526
};
2627

27-
bootstrapInputs =
28-
let
29-
ins = import ./inputs.nix { inherit lib; };
30-
take = name: { flake-file.inputs.${name} = ins.flake-file.inputs.${name}; };
31-
names =
32-
if bootstrap == true then lib.attrNames ins.flake-file.inputs else lib.flatten [ bootstrap ];
33-
in
34-
map take names;
35-
3628
module = {
3729
imports = [
3830
tree
@@ -45,8 +37,11 @@ let
4537
./../write-flake.nix
4638
./../write-lock.nix
4739
./../flake-options.nix
48-
{ imports = bootstrapInputs; }
49-
(if outputs == null then { } else { flake-file.outputs = outputs; })
40+
(lib.optionalAttrs (inputs != null) { flake-file.inputs = inputs; })
41+
(lib.optionalAttrs (outputs != null) { flake-file.outputs = strOrFile outputs; })
42+
(lib.optionalAttrs (do-not-edit != null) { flake-file.do-not-edit = strOrFile do-not-edit; })
43+
44+
bootstrap-app
5045
];
5146
config.flake-file.intoPath = outdir;
5247
options = {
@@ -56,10 +51,34 @@ let
5651
};
5752
};
5853

59-
evaled = lib.evalModules {
60-
modules = [ module ];
61-
specialArgs.inputs.self.outPath = "";
54+
strOrFile = str: if builtins.isPath str then builtins.readFile str else str;
55+
56+
bootstrap-app = {
57+
flake-file.apps.bootstrap =
58+
pkgs:
59+
let
60+
inputs = ''{ flake-file.url = "github:vic/flake-file"; }'';
61+
notice = pkgs.writeText "notice" ''
62+
# DO-NOT-EDIT. Generated by github:vic/flake-file from flake-file.nix. Regen with:
63+
# nix-shell https://github.com/vic/flake-file/archive/main.zip -A flake-file.sh --run bootstrap
64+
'';
65+
in
66+
pkgs.writeShellApplication {
67+
name = "bootstrap";
68+
text = ''
69+
if ! test -f flake-file.nix; then
70+
cp flake.nix flake-file.nix
71+
fi
72+
nix-shell ${../..} -A flake-file.sh --run write-flake \
73+
--arg modules ./flake-file.nix \
74+
--arg do-not-edit ${notice} \
75+
--argstr outputs flake-module \
76+
--arg inputs '${inputs}'
77+
'';
78+
};
6279
};
6380

81+
evaled = lib.evalModules { modules = [ module ]; };
82+
6483
in
6584
evaled.config

modules/bootstrap/inputs.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import-tree.url = lib.mkDefault "github:vic/import-tree";
55
flake-file.url = lib.mkDefault "github:vic/flake-file";
66
flake-parts.url = lib.mkDefault "github:hercules-ci/flake-parts";
7-
flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs";
7+
flake-parts.inputs.nixpkgs-lib.follows = lib.mkDefault "nixpkgs";
88
nixpkgs.url = lib.mkDefault "https://channels.nixos.org/nixpkgs-unstable/nixexprs.tar.xz";
99
};
1010
}

modules/default.nix

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
let
22
flakeModules = {
33
inherit
4-
default
4+
flake # for non-flake-parts flakes
5+
default # for flake-parts flakes (keep as default for compatibility)
56
allfollow
67
nix-auto-follow
78
dendritic
@@ -13,6 +14,13 @@ let
1314
;
1415
};
1516

17+
# A flake without flake-parts. (traditional flake)
18+
flake.imports = [
19+
base
20+
flake-options
21+
./write-flake.nix
22+
];
23+
1624
flake-options = ./flake-options.nix;
1725

1826
base.imports = [

modules/flake-options.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{ lib, config, ... }:
22
let
3-
outputsOption = lib.mkOption { type = lib.type.functionTo outputsType; };
3+
outputsOption = lib.mkOption { type = lib.types.functionTo outputsType; };
44

55
outputsType = lib.types.submoduleWith {
66
modules = [

modules/options/outputs.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let
1313
inputs:
1414
(inputs.nixpkgs.lib.evalModules {
1515
specialArgs = { inherit inputs; inherit (inputs) self; };
16-
modules = [ ./flake-file.nix ];
16+
modules = [ inputs.flake-file.flakeModules.flake ./flake-file.nix ];
1717
}).config.outputs inputs
1818
'';
1919

modules/write-flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ let
8787
name = "write-flake";
8888
text = ''
8989
cd ${config.flake-file.intoPath}
90-
cp --no-preserve=mode ${formatted pkgs} flake.nix
90+
cat ${formatted pkgs} > flake.nix
9191
${hooks}
9292
'';
9393
};

0 commit comments

Comments
 (0)