Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 using clap. Parses arguments and dispatches to command handlers in src/commands/.

  • Library crate (src/lib.rs):

    • flk::flake: generation, parsing and data types
    • flk::devbox: devbox.json import
    • flk::utils: backups, spinner and output helpers
  • src/nix/: runs nix subprocesses.

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 profiles
  • Profile: packages, env vars and commands of one profile
  • Package: name and optional pinned version
  • SourcesSection, OverlaysSection: the two halves of pins.nix