# Development shells as data. Every `shells.` (a perSystem option, set # by the files in modules/dev/shells/) becomes `devShells..`. # A shell can `extends` other shells by name. The shells are linearized, # dependencies first, each one once, the shell itself last, and merged in that # order: packages, hooks and hardening switches add up, a later env value wins, # and the stdenv (the C/C++ compiler) is the last one set, else pkgs.stdenv (GCC). # `default`, `list`, `ls` and `init` cannot be shell names: the first is the # repository shell, the others are commands of `dev`. # # `legacyPackages..composeShells { shells = [ ... ]; packages = [ ... ]; }` # merges shells ad hoc (plus nixpkgs attribute paths). The `dev` helper # (pkgs/dev-shell) uses it for `dev rust cpp -p python3`. # # `devShells.default`, the repository's own shell, stays in modules/flake/dev.nix. { lib, ... }: let inherit (lib) mkOption types; shellType = types.submodule { options = { description = mkOption { type = types.str; default = ""; description = "One line for `dev list` (the shell's meta.description)."; }; extends = mkOption { type = types.listOf types.str; default = [ ]; description = "Shells, by name, merged into this one in order (this shell's own values win)."; }; stdenv = mkOption { type = types.nullOr types.package; default = null; description = "stdenv, i.e. the C/C++ compiler behind cc/c++, CC and CXX. null: the last one set by `extends`, else pkgs.stdenv (GCC)."; }; packages = mkOption { type = types.listOf types.package; default = [ ]; description = "Tools and libraries. mkShell is not strictDeps, so libraries here reach the compiler flags, pkg-config and CMake."; }; env = mkOption { type = types.attrsOf types.str; default = { }; description = "Environment variables."; }; shellHook = mkOption { type = types.lines; default = ""; description = "Bash run when the shell starts (nix develop and direnv)."; }; hardeningDisable = mkOption { type = types.listOf types.str; default = [ ]; description = ''cc-wrapper hardening flags to turn off, e.g. [ "all" ].''; }; }; }; in { perSystem = { config, pkgs, ... }: let specs = config.shells; # Names `dev` or the repository already use. reserved = { default = "`default` is the repository shell (modules/flake/dev.nix)"; list = "`list` is a command of `dev` (pkgs/dev-shell)"; ls = "`ls` is a command of `dev` (pkgs/dev-shell)"; init = "`init` is a command of `dev` (pkgs/dev-shell)"; }; clash = lib.findFirst (n: specs ? ${n}) null (lib.attrNames reserved); # Merge resolved shells left to right. merge = list: { stdenv = lib.foldl' (acc: s: if s.stdenv != null then s.stdenv else acc) null list; packages = lib.concatMap (s: s.packages) list; env = lib.foldl' (acc: s: acc // s.env) { } list; shellHook = lib.concatStringsSep "\n" (lib.filter (h: h != "") (map (s: s.shellHook) list)); hardeningDisable = lib.unique (lib.concatMap (s: s.hardeningDisable) list); }; # The names of a shell and everything it extends, dependencies first, each # name once (its first place), the shell itself last; `chain` detects cycles. linearize = chain: name: if lib.elem name chain then throw "dev shell `${name}` extends itself: ${lib.concatStringsSep " -> " (chain ++ [ name ])}" else if !(specs ? ${name}) then throw "unknown dev shell `${name}`; defined: ${lib.concatStringsSep ", " (lib.attrNames specs)}" else lib.unique (lib.concatMap (linearize (chain ++ [ name ])) specs.${name}.extends ++ [ name ]); # One shell with everything it extends folded in. resolve = name: merge (map (n: specs.${n}) (linearize [ ] name)); build = name: description: s: (pkgs.mkShell.override { stdenv = if s.stdenv != null then s.stdenv else pkgs.stdenv; }) { inherit name; inherit (s) packages shellHook hardeningDisable; # DEV_SHELL names the shell for the prompt (starship) and for `dev`. env = s.env // { DEV_SHELL = name; }; meta.description = description; }; in { options.shells = mkOption { type = types.attrsOf shellType; default = { }; description = "Development shells; each becomes devShells..."; }; config = { devShells = if clash != null then throw "shells.${clash}: ${reserved.${clash}}; choose another name" else lib.mapAttrs (name: spec: build name spec.description (resolve name)) specs; legacyPackages.composeShells = { shells ? [ ], packages ? [ ], }: let name = lib.concatStringsSep "+" (shells ++ packages); extra = map ( p: lib.attrByPath (lib.splitString "." p) (throw "no package `${p}` in nixpkgs") pkgs ) packages; in build (if name == "" then "adhoc" else name) "ad hoc: ${name}" ( merge ( map (n: specs.${n}) (lib.unique (lib.concatMap (linearize [ ]) shells)) ++ [ { stdenv = null; packages = extra; env = { }; shellHook = ""; hardeningDisable = [ ]; } ] ) ); }; }; }