Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use mkNakedShell from devshell for less noisy shell activation #105

Merged
merged 7 commits into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions src/modules/mkNakedShell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# 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
, 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 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 PATH="${profile}/bin:$PATH"

${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
37 changes: 22 additions & 15 deletions src/modules/top-level.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{ config, pkgs, lib, ... }:
let types = lib.types;
let
types = lib.types;
mkNakedShell = pkgs.callPackage ./mkNakedShell.nix { };
in
{
options = {
Expand Down Expand Up @@ -54,24 +56,29 @@ in
enterShell = ''
export PS1="(devenv) $PS1"

# note what environments are active, but make sure we don't repeat them
if [[ ! "$DIRENV_ACTIVE" =~ (^|:)"$PWD"(:|$) ]]; then
export DIRENV_ACTIVE="$PWD:$DIRENV_ACTIVE"
fi
# note what environments are active, but make sure we don't repeat them
if [[ ! "$DIRENV_ACTIVE" =~ (^|:)"$PWD"(:|$) ]]; then
export DIRENV_ACTIVE="$PWD:$DIRENV_ACTIVE"
fi

# devenv helper
if [ ! type -p direnv &>/dev/null && -f .envrc ]; then
echo "You have .envrc but direnv command is not installed."
echo "Please install direnv: https://direnv.net/docs/installation.html"
fi
# devenv helper
if [ ! type -p direnv &>/dev/null && -f .envrc ]; then
echo "You have .envrc but direnv command is not installed."
echo "Please install direnv: https://direnv.net/docs/installation.html"
fi
'';

shell = pkgs.mkShell ({
name = "devenv";
packages = config.packages;
shell = mkNakedShell {
name = "devenv-shell";
env = config.env;
profile = pkgs.buildEnv {
name = "devenv-profile";
paths = config.packages;
ignoreCollisions = true;
};
shellHook = config.enterShell;
} // config.env);
};

ci = [ config.shell config.procfile ];
ci = [ config.shell.inputDerivation config.procfile ];
};
}