From 7bdefce1972365d4f077998769ce5ee82b610206 Mon Sep 17 00:00:00 2001 From: Krisztian Szabo Date: Sat, 25 Mar 2023 00:05:35 +0100 Subject: [PATCH] Revert to pkgs.mkShell + unsetting irrelevant env vars --- src/modules/mkNakedShell.nix | 137 ----------------------------------- src/modules/top-level.nix | 47 ++++++++++-- 2 files changed, 41 insertions(+), 143 deletions(-) delete mode 100644 src/modules/mkNakedShell.nix diff --git a/src/modules/mkNakedShell.nix b/src/modules/mkNakedShell.nix deleted file mode 100644 index 03a1d786a..000000000 --- a/src/modules/mkNakedShell.nix +++ /dev/null @@ -1,137 +0,0 @@ -# using copied code from https://github.com/numtide/devshell -# -# MIT License - -# Copyright (c) 2021 Numtide and contributors - -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: - -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. - -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -{ bashInteractive -, coreutils -, system -, writeTextFile -, pkgs -, lib -}: -let - bashPath = "${bashInteractive}/bin/bash"; - stdenv = writeTextFile { - name = "naked-stdenv"; - destination = "/setup"; - text = '' - # Fix for `nix develop` - : ''${outputs:=out} - - runHook() { - eval "$shellHook" - unset runHook - } - ''; - }; -in -{ name -, # A path to a buildEnv that will be loaded by the shell. - # We assume that the buildEnv contains an ./env.bash script. - profile -, env ? { } -, shellHook ? "" -, meta ? { } -, passthru ? { } -}: -let - # simpler version of https://github.com/numtide/devshell/blob/20d50fc6adf77fd8a652fc824c6e282d7737b85d/modules/env.nix#L41 - envToBash = name: value: "export ${name}=${lib.escapeShellArg (toString value)}"; - startupEnv = lib.concatStringsSep "\n" (lib.mapAttrsToList envToBash env); - derivationArg = { - inherit name system; - - # `nix develop` actually checks and uses builder. And it must be bash. - builder = bashPath; - - # Bring in the dependencies on `nix-build` - args = [ "-ec" "${coreutils}/bin/ln -s ${profile} $out; exit 0" ]; - - # $stdenv/setup is loaded by nix-shell during startup. - # https://github.com/nixos/nix/blob/377345e26f1ac4bbc87bb21debcc52a1d03230aa/src/nix-build/nix-build.cc#L429-L432 - stdenv = stdenv; - - # The shellHook is loaded directly by `nix develop`. But nix-shell - # requires that other trampoline. - shellHook = '' - # Remove all the unnecessary noise that is set by the build env - unset NIX_BUILD_TOP NIX_BUILD_CORES NIX_STORE - unset TEMP TEMPDIR TMP ${lib.optionalString (!pkgs.stdenv.isDarwin) "TMPDIR"} - # $name variable is preserved to keep it compatible with pure shell https://github.com/sindresorhus/pure/blob/47c0c881f0e7cfdb5eaccd335f52ad17b897c060/pure.zsh#L235 - unset builder out shellHook stdenv system - # Flakes stuff - unset dontAddDisableDepTrack outputs - - # For `nix develop`. We get /noshell on Linux and /sbin/nologin on macOS. - if [[ "$SHELL" == "/noshell" || "$SHELL" == "/sbin/nologin" ]]; then - export SHELL=${bashPath} - fi - - # https://github.com/numtide/devshell/issues/158 - PATH=''${PATH#/path-not-set:} - - export DEVENV_PROFILE=${profile} - - # add installed packages to PATH - export PATH="$DEVENV_PROFILE/bin:$PATH" - - # prepend common compilation lookup paths - export PKG_CONFIG_PATH="$DEVENV_PROFILE/lib/pkgconfig:''${PKG_CONFIG_PATH-}" - export LD_LIBRARY_PATH="$DEVENV_PROFILE/lib:''${LD_LIBRARY_PATH-}" - export LIBRARY_PATH="$DEVENV_PROFILE/lib:''${LIBRARY_PATH-}" - export C_INCLUDE_PATH="$DEVENV_PROFILE/include:''${C_INCLUDE_PATH-}" - - # these provide shell completions / default config options - export XDG_DATA_DIRS="$DEVENV_PROFILE/share:''${XDG_DATA_DIRS-}" - export XDG_CONFIG_DIRS="$DEVENV_PROFILE/etc/xdg:''${XDG_CONFIG_DIRS-}" - - ${startupEnv} - - ${shellHook} - ''; - }; -in -(derivation derivationArg) // { - inherit meta passthru; - - # https://github.com/NixOS/nixpkgs/blob/41f7e338216fd7f5e57817c4f8e148d42fb88b24/pkgs/stdenv/generic/make-derivation.nix#L486-L504 - inputDerivation = derivation (derivationArg // { - # Add a name in case the original drv didn't have one - name = derivationArg.name or "inputDerivation"; - # This always only has one output - outputs = [ "out" ]; - - # Propagate the original builder and arguments, since we override - # them and they might contain references to build inputs - _derivation_original_builder = derivationArg.builder; - _derivation_original_args = derivationArg.args; - - builder = bashPath; - # The bash builtin `export` dumps all current environment variables, - # which is where all build input references end up (e.g. $PATH for - # binaries). By writing this to $out, Nix can find and register - # them as runtime dependencies (since Nix greps for store paths - # through $out to find them) - args = [ "-c" "export > $out" ]; - }); -} // passthru diff --git a/src/modules/top-level.nix b/src/modules/top-level.nix index 76e7e46fb..ad0685e88 100644 --- a/src/modules/top-level.nix +++ b/src/modules/top-level.nix @@ -1,7 +1,6 @@ { config, pkgs, lib, ... }: let types = lib.types; - mkNakedShell = pkgs.callPackage ./mkNakedShell.nix { }; # Returns a list of all the entries in a folder listEntries = path: map (name: path + "/${name}") (builtins.attrNames (builtins.readDir path)); @@ -61,6 +60,42 @@ in default = [ ]; }; + unsetEnvVars = lib.mkOption { + type = types.listOf types.str; + description = "Remove these list of env vars from being exported to keep the shell/direnv more lean."; + # manually determined with knowledge from https://nixos.wiki/wiki/C + default = [ + "HOST_PATH" + "NIX_BUILD_CORES" + "__structuredAttrs" + "buildInputs" + "buildPhase" + "builder" + "depsBuildBuild" + "depsBuildBuildPropagated" + "depsBuildTarget" + "depsBuildTargetPropagated" + "depsHostHost" + "depsHostHostPropagated" + "depsTargetTarget" + "depsTargetTargetPropagated" + "doCheck" + "doInstallCheck" + "nativeBuildInputs" + "out" + "outputs" + "patches" + "phases" + "preferLocalBuild" + "propagatedBuildInputs" + "propagatedNativeBuildInputs" + "shell" + "shellHook" + "stdenv" + "strictDeps" + ]; + }; + shell = lib.mkOption { type = types.package; internal = true; @@ -132,21 +167,21 @@ in mkdir -p .devenv rm -f .devenv/profile ln -s ${profile} .devenv/profile + unset ${lib.concatStringsSep " " config.unsetEnvVars} ''; shell = performAssertions ( - mkNakedShell { + pkgs.mkShell ({ name = "devenv-shell"; - env = config.env; - profile = profile; + packages = [ profile ]; shellHook = config.enterShell; - } + } // config.env) ); infoSections."env" = lib.mapAttrsToList (name: value: "${name}: ${toString value}") config.env; infoSections."packages" = builtins.map (package: package.name) (builtins.filter (package: !(builtins.elem package.name (builtins.attrNames config.scripts))) config.packages); - ci = [ config.shell.inputDerivation ]; + ci = [ config.shell ]; ciDerivation = pkgs.runCommand "ci" { } ("ls " + toString config.ci + " && touch $out"); }; }