With a flake-parts flake
Suppose you have a flake-parts enabled flake that looks like:
{ 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.
Split flake.nix in two
Section titled “Split flake.nix in two”First step is moving the (B) flake-parts entry-point module
outside of the flake.nix file into modules/default.nix.
{ imports = [ ./inputs.nix ]; systems = [ "aarch64-darwin" ];
... # same code as before}Second is also moving the (A) inputs into ./modules
# 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 };}Boostraping using ./modules
Section titled “Boostraping using ./modules”And now, lets replace flake.nix using bootstrapping magic step:
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-partsSee also: all bootstrap command args
Run the same command whenever your input changes and they
will be reflected into the static flake.nix.
Enabling .#write-flake app.
Section titled “Enabling .#write-flake app.”Edit ./modules/inputs.nix module to add in-flake flake-file.
{ 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"; }Your flake is flake-file enabled
Section titled “Your flake is flake-file enabled”Commands for your daily life:
nix run .#write-flake # whenever you need to regen flake.nix
nix flake check # will make sure your flake.nix is up-to-date