From e6bef50b3780ee96bda60f819453fcbc061c5170 Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Thu, 24 Apr 2025 19:36:49 +0200 Subject: [PATCH] split dev into config and overrides, hook in via impure.nix --- .envrc | 6 +++ .gitignore | 3 +- config.nix | 6 +++ justfile | 29 +++--------- dev.nix => overlays.nix | 98 +++++++++++++++++++++-------------------- 5 files changed, 69 insertions(+), 73 deletions(-) create mode 100644 config.nix rename dev.nix => overlays.nix (87%) diff --git a/.envrc b/.envrc index a7ba5e6..d7a95e3 100644 --- a/.envrc +++ b/.envrc @@ -47,3 +47,9 @@ printf "%s\n" "$now" >.direnv/pull-date direnv allow # cheeky ) + +export NIX_PATH_ORIG="${NIX_PATH:-}" + +# pkgs/top-level/impure.nix go brrr +export NIXPKGS_CONFIG="$(realpath config.nix)" +export NIX_PATH="$NIX_PATH${NIX_PATH:+:}nixpkgs-overlays=$(realpath overlays.nix)" diff --git a/.gitignore b/.gitignore index 249b310..af2b772 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ * !/justfile -!/dev.nix +!/config.nix +!/overlays.nix !/.gitignore !/.envrc diff --git a/config.nix b/config.nix new file mode 100644 index 0000000..3eaa193 --- /dev/null +++ b/config.nix @@ -0,0 +1,6 @@ +{ + allowUnfree = true; + # allowInsecure = true; + # allowBroken = true; + checkMeta = true; +} diff --git a/justfile b/justfile index 49ca6cd..1f8f1b1 100644 --- a/justfile +++ b/justfile @@ -7,6 +7,11 @@ export invokedir := invocation_directory() export epoch := `date +%s` export GUM_FILTER_HEIGHT := "15" export GIT := `command -v git-wait >/dev/null && echo "git-wait" || echo "git"` +# will trigger a direnv reload when we enter $SHELL +export DIRENV_WATCHES := "" +# please no surprises for now +export NIXPKGS_CONFIG := "" +export NIX_PATH := `echo "${NIX_PATH_ORIG:-"${NIX_PATH:-}"}"` # required programs: git nix nom gum fzf tee nix-update @@ -730,27 +735,3 @@ _packages_json: @_fmt: just --unstable --fmt #sd "\n+(@_[a-zA-Z_-]+:)" "\n" justfile - -#sync ./dev.nix -dev: - #!/usr/bin/env -S bash -euo pipefail - [[ -f "$invokedir"/flake.nix - && -f "$invokedir"/default.nix - && -e "$invokedir"/.git - && -f "$invokedir"/pkgs/top-level/impure.nix - ]] || ! echo >&2 "ERROR: not in nixpkgs root" - - # local .gitignore for dev.nix - if ! grep -qxF "/dev.nix" master/.git/info/exclude; then - echo "/dev.nix" >> master/.git/info/exclude - fi - - if [[ ! -f "$invokedir"/dev.nix ]]; then - (set -x; cp dev.nix "$invokedir"/dev.nix) - elif [[ dev.nix -nt "$invokedir"/dev.nix ]]; then - (set -x; cp -f dev.nix "$invokedir"/dev.nix) - elif diff -qs dev.nix "$invokedir"/default.nix; then - : - else - ! echo >&2 "dunnolol" - fi diff --git a/dev.nix b/overlays.nix similarity index 87% rename from dev.nix rename to overlays.nix index 6d2f871..96eca69 100755 --- a/dev.nix +++ b/overlays.nix @@ -1,30 +1,9 @@ #!/usr/bin/env -S nix-instantiate --parse -{ ... }@cliArgs: let - lib = import ./lib; + lib = import ./master/lib; # merge = sets: lib.foldl' lib.recursiveUpdate {} sets; - apply = val: f: f val; appendIfMissing = list: val: if builtins.elem val list then list else list ++ [ val ]; - mkPkgs = - args: - import ./. ( - apply (lib.recursiveUpdate cliArgs args) (prev: rec { - # system = ; - # crossSystem = ; - config.allowUnfree = true; - # config.allowInsecure = true; - # config.allowBroken = true; - config.checkMeta = true; - overlays = prev.overlays or [ ] ++ [ - overlay.withShellCheck - overlay.withDebug - overlay.withTwostagePythonBuildPassthru - # overlay.withPytestCheckHooks - ]; - }) - ); - mkDerivationOverlay = let # from pkgs/stdenv/adapters.nix @@ -48,7 +27,9 @@ let # TODO: need we override any other stdenvs? mkStdenvOverlay = f: final: prev: { - stdenv = (prev.addAttrsToDerivation { } prev.stdenv).override (f final prev prev.stdenv.mkDerivation); + stdenv = (prev.addAttrsToDerivation { } prev.stdenv).override ( + f final prev prev.stdenv.mkDerivation + ); # stdenvNoCC = (prev.addAttrsToDerivation { } prev.stdenvNoCC).override (f final prev prev.stdenvNoCC.mkDerivation); # stdenvLLVM = (prev.addAttrsToDerivation { } prev.stdenvLLVM).override (f final prev prev.stdenvLLVM.mkDerivation); }; @@ -67,7 +48,7 @@ let let stdenv' = stdenv // { # mkDerivation = stdenv.mkDerivationFromStdenv stdenv; - mkDerivation = prevMkDerivation; + mkDerivation = prevMkDerivation; # HACK }; in f final prev stdenv' finalAttrs prevAttrs @@ -78,16 +59,16 @@ let # none of these overlays should cause a mass-rebuild, they should only add optional functionality + # TODO: cccache and sccache # TODO: fetchurl.passthru.unpacked # TODO: mkDerivation.passthru.withOil: add .withOil which run build with osh, https://oils.pub/osh.html#2-you-get-precise-error-messages # TODO: mkDerivation.passthru.withGDB: add .withGDB which runs the full build with gdb # TODO: mkDerivation.passthru.withDebInfo: like overlay.withDebug but with cmakeBuildType = "RelWithDebInfo" mode # usage: nix-build ./dev.nix -A .withDebug - overlay.withDebug = mkDerivationOverlay ( + overlays.withDebug = mkDerivationOverlay ( final: prev: prevStdenv: finalAttrs: prevAttrs: { passthru = { - # withDebug = prevStdenv; withDebug = prevStdenv.mkDerivation ( prevAttrs // { @@ -102,7 +83,7 @@ let ); # usage: nix-build ./dev.nix -A .shellcheck - overlay.withShellCheck = mkDerivationOverlay ( + overlays.withShellCheck = mkDerivationOverlay ( final: prev: prevStdenv: finalAttrs: prevAttrs: let snippets = lib.pipe finalAttrs [ @@ -159,23 +140,24 @@ let # very hacky, not guaranteed to work, but may save a lot of .whl rebuilds # usage: nix-build ./dev.nix -A python3Packages..twostage - overlay.withTwostagePythonBuildPassthru = final: prev: { + # usage: nix-build ./dev.nix -A python3Packages..twostage.first + overlays.withTwostagePythonBuildPassthru = final: prev: { pythonPackagesExtensions = prev.pythonPackagesExtensions ++ [ ( pythonFinal: pythonPrev: let mkTwostage = - builder: + buildPythonPackage: ( args: - builder ( + buildPythonPackage ( args // { passthru = { twostage = let # TODO: assert "dist" in finalPackage.outputs? - first = builder ( + first = buildPythonPackage ( args // { name = "${args.name or "${args.pname}-${args.version}"}-wheels"; @@ -210,7 +192,7 @@ let postInstallCheck = null; } ); - second = builder ( + second = buildPythonPackage ( args // { buildPhase = '' @@ -240,7 +222,7 @@ let }; # usage: nativeBuildInputs = [ pytestCheckHookWithGdb ]; - overlay.withPytestCheckHooks = final: prev: { + overlays.withPytestCheckHooks = final: prev: { pythonPackagesExtensions = prev.pythonPackagesExtensions ++ [ (pythonFinal: pythonPrev: { pytestCheckHookWithGdb = pythonFinal.pytestCheckHook.override (old: { @@ -294,19 +276,39 @@ let ]; }; + overlays.toplevelCudaRocmPkgs = + final: prev: + let + mkPkgs = + { + config ? { }, + # overlays ? [ ], + }: + import prev.path { + # if we don't specify overlays then impure.nix will do its thing + # overlays = prev.overlays + overlays; + config = prev.config // config; + }; + in + { + pkgsCuda = mkPkgs { config.cudaSupport = true; }; + pkgsRocm = mkPkgs { config.rocmSupport = true; }; + # pkgsDarwin? + # pkgsLinux? + # pkgsFreeBSD? + # pkgsNetBSD? + # pkgsOpenBSD? + # pkgsWindows? + }; + in -(mkPkgs { }) -// { - pkgsCuda = mkPkgs { - config.enableCuda = true; - }; - pkgsRocm = mkPkgs { - config.enableRocm = true; - }; - # pkgsDarwin - # pkgsLinux - # pkgsFreeBSD - # pkgsNetBSD - # pkgsOpenBSD - # pkgsWindows -} +[ + overlays.withShellCheck + overlays.withDebug + overlays.withInvalidateFetcherByDrvHash + overlays.withTwostagePythonBuildPassthru + overlays.withPytestCheckHooks + overlays.toplevelCudaRocmPkgs + # WIP: + # overlays.withCudaOrRocm +]