all-in-one: merge entrypoint into the devshell dir (#121)

Split the entrypoint and env.bash. This makes it easier to expose the
buildEnv.
This commit is contained in:
Jonas Chevalier 2021-09-23 23:10:54 +02:00 committed by GitHub
parent dd0d585d2e
commit 5a93060a2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 43 additions and 38 deletions

2
.envrc
View File

@ -14,4 +14,4 @@ nix-build shell.nix --out-link "$out_link"
# Load the devshell
# shellcheck disable=SC1090
source "$out_link"
source "$out_link/env.bash"

View File

@ -18,7 +18,8 @@ jobs:
with:
name: numtide
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
- run: $(nix-build shell.nix) --pure /usr/bin/env HOME=$HOME bash -c "cd devshell && golangci-lint run"
- run: |
$(./shell.nix)/entrypoint --pure /usr/bin/env HOME=$HOME bash -c "cd devshell && golangci-lint run"
- run: nix-shell --run "echo OK"
- run: nix-build
flakes:

View File

@ -22,7 +22,7 @@ let
pkgs.stdenvNoCC.mkDerivation {
name = "devshell-setup-hook";
setupHook = pkgs.writeText "devshell-setup-hook.sh" ''
source ${entrypoint}
source ${devshell_dir}/env.bash
'';
dontUnpack = true;
dontBuild = true;
@ -58,8 +58,11 @@ let
# Write a bash profile to load
envBash = pkgs.writeText "devshell-env.bash" ''
# It assums that the shell is always loaded from the root of the project.
export DEVSHELL_ROOT=''${DEVSHELL_ROOT:-$PWD}
# Expose the folder that contains the assembled environment.
export DEVSHELL_DIR=@devshellDir@
export DEVSHELL_DIR=@DEVSHELL_DIR@
# Prepend the PATH with the devshell dir and bash
PATH=''${PATH%:/path-not-set}
@ -78,29 +81,18 @@ let
fi # Interactive session
'';
# Builds the DEVSHELL_DIR with all the dependencies
devshellDir = pkgs.buildEnv {
name = "devshell-dir";
paths = cfg.packages;
postBuild = ''
cp ${envBash} $out/env.bash
substituteInPlace $out/env.bash --subst-var-by devshellDir $out
'';
};
# This is our entry-point for everything!
# This is our entrypoint script.
entrypoint = pkgs.writeShellScript "${cfg.name}-entrypoint" ''
#!${bashPath}
# Script that sets-up the environment. Can be both sourced or invoked.
# It assums that the shell is always loaded from the root of the project.
# Store that for later usage.
export DEVSHELL_ROOT=$PWD
export DEVSHELL_DIR=@DEVSHELL_DIR@
# If the file is sourced, skip all of the rest and just source the env
# script.
if [[ $0 != "''${BASH_SOURCE[0]}" ]]; then
source "${devshellDir}/env.bash"
source "$DEVSHELL_DIR/env.bash"
return
fi
@ -109,11 +101,10 @@ let
if [[ $# = 0 ]]; then
# Start an interactive shell
exec "${bashPath}" --rcfile "${devshellDir}/env.bash" --noprofile
exec "${bashPath}" --rcfile "$DEVSHELL_DIR/env.bash" --noprofile
elif [[ $1 == "-h" || $1 == "--help" ]]; then
cat <<USAGE
Usage: ${cfg.name}
source $0 # load the environment in the current bash
$0 -h | --help # show this help
$0 [--pure] # start a bash sub-shell
$0 [--pure] <cmd> [...] # run a command in the environment
@ -128,10 +119,22 @@ let
exec -c "$0" "$@"
else
# Start a script
source "${devshellDir}/env.bash"
source "$DEVSHELL_DIR/env.bash"
exec -- "$@"
fi
'';
# Builds the DEVSHELL_DIR with all the dependencies
devshell_dir = pkgs.buildEnv {
name = "devshell-dir";
paths = cfg.packages;
postBuild = ''
substitute ${envBash} $out/env.bash --subst-var-by DEVSHELL_DIR $out
substitute ${entrypoint} $out/entrypoint --subst-var-by DEVSHELL_DIR $out
chmod +x $out/entrypoint
'';
};
in
{
options.devshell = {
@ -143,11 +146,11 @@ in
description = "Version of bash to use in the project";
};
entrypoint = mkOption {
package = mkOption {
internal = true;
type = types.package;
description = ''
This package contains a script that loads the current environment.
This package contains the DEVSHELL_DIR
'';
};
@ -220,7 +223,7 @@ in
};
config.devshell = {
entrypoint = entrypoint;
package = devshell_dir;
startup = {
load_profiles = noDepEntry ''
@ -283,7 +286,7 @@ in
# Use a naked derivation to limit the amount of noise passed to nix-shell.
shell = mkNakedShell {
name = cfg.name;
script = cfg.entrypoint;
profile = cfg.package;
passthru = {
inherit config;
flakeApp = mkFlakeApp entrypoint;

View File

@ -20,8 +20,9 @@ let
};
in
{ name
, # A path to a script that will be loaded by the shell
script
, # A path to a buildEnv that will be loaded by the shell.
# We assume that the buildEnv contains an ./env.bash script.
profile
, meta ? { }
, passthru ? { }
}:
@ -32,7 +33,7 @@ in
builder = bashPath;
# Bring in the dependencies on `nix-build`
args = [ "-ec" "${coreutils}/bin/ln -s ${script} $out; exit 0" ];
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
@ -53,7 +54,7 @@ in
export SHELL=${bashPath}
fi
# Load the script environment
source "${script}"
# Load the environment
source "${profile}/env.bash"
'';
}) // { inherit meta passthru; } // passthru

View File

@ -31,7 +31,7 @@
in
runTest "devshell-1" { } ''
# Load the devshell
source ${shell}
source ${shell}/env.bash
menu

View File

@ -10,7 +10,7 @@
in
runTest "devshell-1" { } ''
# Load the devshell
source ${shell}
source ${shell}/env.bash
# Sets an environment variable that points to the buildEnv
assert -n "$DEVSHELL_DIR"

View File

@ -25,7 +25,7 @@
unset XDG_DATA_DIRS
# Load the devshell
source ${shell}
source ${shell}/env.bash
# NIXPKGS_PATH is being set
assert "$NIXPKGS_PATH" == "${toString pkgs.path}"

View File

@ -26,7 +26,7 @@
assert_fail -L .git/hooks/pre-commit
# Load the devshell
source ${shell1}
source ${shell1}/env.bash
# The hook has been install
assert -L .git/hooks/pre-commit
@ -35,7 +35,7 @@
assert "$(.git/hooks/pre-commit)" == "PRE-COMMIT"
# Load the new config
source ${shell2}
source ${shell2}/env.bash
# The hook should have been uninstalled
assert_fail -L .git/hooks/pre-commit

View File

@ -10,7 +10,7 @@
in
runTest "language-c-1" { } ''
# Load the devshell
source ${shell}
source ${shell}/env.bash
# Has a C compiler

View File

@ -10,7 +10,7 @@
in
runTest "simple" { } ''
# Load the devshell
source ${shell}
source ${shell}/env.bash
# Has a rust compiler
type -p rustc

View File

@ -13,7 +13,7 @@
assert -z "$LOCALE_ARCHIVE"
# Load the devshell
source ${shell}
source ${shell}/env.bash
# Sets LOCALE_ARCHIVE
if [[ $OSTYPE == linux-gnu ]]; then