Architecture
Overview
flk is a Rust CLI that reads and edits flake.nix and the files in .flk/.
Crate structure
-
Binary crate (
src/main.rs): CLI entrypoint usingclap. Parses arguments and dispatches to command handlers insrc/commands/. -
Library crate (
src/lib.rs):flk::flake: generation, parsing and data typesflk::devbox:devbox.jsonimportflk::utils: backups, spinner and output helpers
-
src/nix/: runsnixsubprocesses.
Parser design
Parsers in src/flake/parsers/ use the nom library to read and modify specific sections of Nix files:
They parse only the sections flk writes (packages, env vars, commands, pins), not whole Nix files. Each parser returns the entries plus their byte positions and indentation, so an edit replaces one span and leaves comments and formatting elsewhere alone.
Template system
Templates in templates/ are embedded at compile time via include_str!:
src/flake/generator.rs picks one by project type. See Project Templates for the list.
Data layout
project/
├── flake.nix # calls flk.lib.mkProject
├── flake.lock
└── .flk/
├── config.nix # defaultProfile, maxCombinations, allowUnfree
├── pins.nix # version pins
├── profiles/
│ ├── rust.nix
│ └── ...
└── backups/ # flake.lock backups
Projects created with flk init --legacy also have .flk/default.nix, .flk/overlays.nix and .flk/profiles/default.nix. flk migrate converts them. On a slim project created before it declared nixpkgs, flk migrate adds that input and makes flk follow it. flk migrate --global does the same for the global environment.
Command flow
src/main.rs parses arguments and calls a handler in src/commands/. Handlers read and edit .flk/ files through the parsers, call nix through src/nix/, and print through src/utils/visual.rs.
Key interfaces
FlakeConfig: inputs and profilesProfile: packages, env vars and commands of one profilePackage: name and optional pinned versionSourcesSection,OverlaysSection: the two halves ofpins.nix