Skip to content
baskingspotPublic

About

Shared flake-parts modules for baskingspot development environments.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

nix

Shared flake-parts modules for baskingspot development environments.

Every baskingspot repo needs the same flake skeleton and the same tools. This repo exports them as flake-parts modules. Each module adds one devShells.<name>, and a repo composes the ones it wants with inputsFrom.

Modules

  • base: the three supported systems, and nix fmt through treefmt, for Nix files only. It formats with nixfmt and fixes with statix and deadnix. The devshell has treefmt, just, fd, gh, oxfmt (it formats the Markdown and the workflows in every repo's lint), and the workflow tools actionlint, zizmor, and pinact, because every repo is a GitHub repo. It also has ast-grep and baskingspot-lint, which runs the house rules.
  • terraform: OpenTofu and tflint. tofu fmt formats, and tflint and tofu validate check.
  • shell: shellcheck and shfmt. Shell scripts are always checked.
  • docker: the Docker CLI with buildx, and hadolint. Dockerfiles are always checked. The daemon is not in the shell.
  • docs: a docs site on the baskingspot theme: Zola, a pinned wrangler, and the command baskingspot-docs. It is not a language. See docs/README.md.
  • zig: Zig, the matching zls, ziglint, and zigdoc. A version's shell is devShells."zig-0-16" or devShells."zig-0-17", and a repo imports the versions it supports. The dot becomes a hyphen because nix develop .#zig-0.16 reads the dot as an attrpath separator. Each version also exposes ziglint-0-16 or ziglint-0-17, which runs ziglint against that version's standard library; a repo that supports two versions lints with the oldest, so the newer version's deprecations do not fire. The module also reads the project's floor with zig.fromBuildZon.

Lint

Nix has no nix lint. The closest standard is nix fmt, so base puts the Nix linters behind it with treefmt-nix.

  • nix fmt formats Nix files and applies the statix and deadnix fixes.
  • nix fmt -- --ci changes nothing and fails if a Nix file would change.
  • nix flake check runs the same check.

nix fmt touches *.nix and nothing else. Every other language stays with its own toolchain, run from the repo's justfile: zig fmt and ziglint, oxfmt for Markdown, actionlint and zizmor for workflows, and just --fmt.

The house rules are ast-grep rules under pkgs/ast-grep-rules/rules/, one directory per language. baskingspot-lint src runs all of them, then any in the repo's own .ast-grep/rules/. A rule that applies to every repo is added here once; a repo runs the same command with no other change. Each rule carries valid and invalid Zig cases in pkgs/ast-grep-rules/tests/; just test-rules runs them, and nix flake check runs the same test in CI.

Use

Start a new library from the template:

nix flake init -t github:baskingspot/nix#zig-library

Or add the modules to an existing flake:

imports = with inputs.baskingspot.flakeModules; [
  base
  zig."0.16"
];

perSystem = { pkgs, config, ... }: {
  devShells.default = pkgs.mkShell {
    inputsFrom = with config.devShells; [ base zig ];
    packages = with pkgs; [ ];
  };
};

The imports list and the inputsFrom list mirror each other. Pick the Zig version once, in the import.

Zig

A repo with a build.zig.zon does not name a Zig version at all:

imports = with inputs.baskingspot.flakeModules; [
  base
  (zig.fromBuildZon ./build.zig.zon)
];

minimum_zig_version names the release line, and the newest patch in that line is the toolchain. A floor of 0.17.0 gives 0.17.1 today and 0.17.2 when it ships, and it never crosses into 0.18, because a Zig minor does not keep the previous one's API. The shell is devShells.zig.

A repo that supports more than one version imports each one it wants. The names do not collide:

imports = with inputs.baskingspot.flakeModules; [
  base
  zig."0.16"
  zig."0.17"
];

Zig is not TOML, so the field is read off its line rather than parsed. A commented-out value does not win, and a file without the field is an evaluation error.

The template is copied before zig init writes the manifest, so it passes a fallback:

(zig.fromBuildZon { file = ./build.zig.zon; fallback = "0.17"; })

A tree with no build.zig.zon yet gets that release line, and the floor takes over as soon as the file exists.

Without the modules

A flake that does not use these modules takes the overlay:

inputs.baskingspot = {
  url = "github:baskingspot/nix";
  inputs.nixpkgs.follows = "nixpkgs";
};

# ...
pkgs = import nixpkgs {
  inherit system;
  overlays = [ baskingspot.overlays.default ];
};

Then pkgs.zig-bin answers in four shapes:

pkgs.zig-bin."0.17.1"                     # exactly that release
pkgs.zig-bin."0.17"                       # the newest 0.17.x
pkgs.zig-bin.latest                       # the newest release
pkgs.zig-bin.fromBuildZon ./build.zig.zon # the floor's line, newest patch

Each one is a toolchain set:

Attribute What it is
.zig the compiler, with the setup hook, so a derivation puts this in nativeBuildInputs
.zls the language server built for that Zig, or null when there is no pairing
.ziglint ziglint, or null on a system without a release
.zigdoc zigdoc, or null on a system without a release
.minimal the compiler again
.default zig and zls
.complete zig, zls, ziglint, and zigdoc

.default and .complete are joins, so they are for shells. They do not carry the setup hook. A derivation wants .zig.

Where the binaries come from

pkgs/zig-toolchain/data/ holds every version and hash, and the fetch tries zig.baskingspot.dev first and the publisher second. The Zig tarball is checked against the Zig Software Foundation's minisign key and the file name in the trusted comment; zls is checked against the zigtools key. A mirror can serve other bytes; it cannot sign them.

The release line is 0.16 and 0.17 today. The supported systems are x86_64-linux, aarch64-linux, and aarch64-darwin.

The target is named, not detected

The official Zig binary finds the host libc by reading /usr/bin/env at run time. A Nix build sandbox has no /usr, so that detection fails and the compiler falls back to musl. Zig tracks the problem as ziglang/zig#15898, and a prebuilt Zig cannot fix it from the inside: nixpkgs' Zig works only because it is compiled from source after that path is patched.

So the setup hook names the target and the interpreter on Linux, the same way nixpkgs' hook names the CPU baseline:

-Dtarget=aarch64-linux-gnu.2.44
-Ddynamic-linker=/nix/store/.../ld-linux-aarch64.so.1

Naming a target stops Zig emitting an rpath for each -L path. A project that links libraries from nixpkgs sets one line on its compile step:

executable.each_lib_rpath = true;

z53 does that, and its package builds with the toolchain like any other. A project that sets dontSetZigDefaultFlags takes over all four flags.

Work on it

nix develop provides the tools, and direnv allow loads them. just lists the recipes, and just ci runs what CI runs.

License

MIT. See LICENSE. The logo files in docs/static/ are baskingspot marks and are not covered by it. See docs/static/LICENSE.

How it's made

Built with LLM assistance. How, in detail.

baskingspot

About

Shared flake-parts modules for baskingspot development environments.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages