Skip to content

With a flake-parts flake

Suppose you have a flake-parts enabled flake that looks like:

flake.nix
{
inputs = { # (A) So much inputs
nixpkgs.url = "github:nixos/nixpkgs/unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
foo.url = "github:coolguy/foo";
bar.url = "github:coolguy/bar";
};
outputs = inputs:
inputs.flake-parts.lib.mkFlake { inherit inputs; }
# (B) flake-parts entry-point module.
{ systems = [ "aarch64-darwin" ]; ... };
}

Your flake is already modular (in this case flake-parts modules), so your migration path is smoother here.

First step is moving the (B) flake-parts entry-point module outside of the flake.nix file into modules/default.nix.

modules/default.nix
{
imports = [ ./inputs.nix ];
systems = [ "aarch64-darwin" ];
... # same code as before
}

Second is also moving the (A) inputs into ./modules

modules/inputs.nix
# This is now real Nix code, use let bindings, { lib, ... } etc.
# best practice is to split this inputs.nix into other modules
{
flake-file.inputs = {
nixpkgs.url = "github:nixos/nixpkgs/unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
# all your other inputs
};
}

And now, lets replace flake.nix using bootstrapping magic step:

Terminal window
nix-shell https://github.com/vic/flake-file/archive/main.tar.gz \
-A flake-file.sh --run write-flake \
--arg modules ./modules --argstr outputs flake-parts
See also: all bootstrap command args

Run the same command whenever your input changes and they will be reflected into the static flake.nix.

Edit ./modules/inputs.nix module to add in-flake flake-file.

modules/inputs.nix
{ inputs, lib, ... }: # resolved flake inputs as specialArgs
{
# same inputs code as before
flake-file.inputs = {
# make sure you add flake-file dependency.
flake-file.url = lib.mkDefault "github:vic/flake-file";
};
imports = [
# enable inside-flake and say goodbye to bootstrap
inputs.flake-file.flakeModule
# start splitting from inputs.nix into other files
];
# generate the same output function we used at bootstrap
flake-file.outputs = "flake-parts";
}

Commands for your daily life:

Terminal window
nix run .#write-flake # whenever you need to regen flake.nix
nix flake check # will make sure your flake.nix is up-to-date
Contribute Community Sponsor