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

shell: use naked shell concept with env var prepends for leaner shell with autocompletion #191

Merged
merged 21 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
8696634
Revert "Revert "Merge pull request #105 from thenonameguy/naked-shell""
thenonameguy Nov 28, 2022
42e4964
Merge branch 'main' of github.com:cachix/devenv into naked-shell
thenonameguy Dec 4, 2022
1641124
Add common env vars to mkNakedShell
thenonameguy Dec 5, 2022
218514a
Fix indentation
thenonameguy Dec 5, 2022
dbc9b17
Update flake.lock
thenonameguy Dec 5, 2022
fdc015a
Merge branch 'naked-shell' of github.com:thenonameguy/devenv into nak…
thenonameguy Dec 7, 2022
ead27c2
Merge branch 'main' of github.com:cachix/devenv into naked-shell
thenonameguy Dec 7, 2022
3e1685e
Merge branch 'main' of github.com:cachix/devenv into naked-shell
thenonameguy Dec 14, 2022
f55b647
Add services.postgres.initialDatabases option
thenonameguy Dec 17, 2022
2030aaf
Use nix-direnv in flake template for maximum speed
thenonameguy Dec 17, 2022
63074e6
Include devenv package in flake usage scenario, fix template
thenonameguy Dec 17, 2022
186deb9
Merge branch 'main' of github.com:cachix/devenv into naked-shell
thenonameguy Dec 17, 2022
f7b5e92
Use pg_isready on DB that always exists
thenonameguy Dec 17, 2022
fa3d586
Merge branch 'main' of github.com:cachix/devenv into naked-shell
thenonameguy Dec 19, 2022
cebf544
add docs about $DEVENV_PROFILE
thenonameguy Dec 19, 2022
a5a304f
Include all package outputs by default in profile
thenonameguy Dec 19, 2022
0a1333b
Optionally depend on nix-direnv hint users about #speed
thenonameguy Dec 19, 2022
d2947ee
Fix postgres readiness probe
thenonameguy Dec 19, 2022
ccee09d
Update process-compose example to use new packages flattening
thenonameguy Dec 19, 2022
1d7aa5a
Merge branch 'naked-shell' of github.com:thenonameguy/devenv into nak…
thenonameguy Dec 20, 2022
4717da8
Automatically install nix-direnv, fixes #133
thenonameguy Dec 20, 2022
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
7 changes: 6 additions & 1 deletion docs/files-and-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ Points to `$DEVENV_ROOT/.devenv`.

### $DEVENV_STATE

Points to `$DEVENV_DOTFILE/state`.
Points to `$DEVENV_DOTFILE/state`.

### $DEVENV_PROFILE

Points to the Nix store path that has final profile of packages/scripts provided by devenv.
Useful for teaching other programs about `/bin`, `/etc`, `/var` folders.
60 changes: 58 additions & 2 deletions docs/reference/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -3800,7 +3800,7 @@ attribute set of attribute set of (INI atom (null, bool, int, float or string) o


## services.postgres.createDatabase
Create a database named like current user on startup.
Create a database named like current user on startup. Only applies when initialDatabases is an empty list.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe makes more sense to remove it and the user migrates to the new option :)

Copy link
Contributor Author

@thenonameguy thenonameguy Dec 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not want to break the current users of this flag.
Also there is no way of specifying at the moment that $USER with a program fallback should be used with the initialDatabases syntax. The name is static at Nix eval time.
In my personal projects I almost always have a static database name locally, so it can be hard-coded for maximum ease of use. This way we don't have to pass the $USER everywhere.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it makes sense to deprecate a flag you can do it like:

imports = [
    (lib.mkRenamedOptionModule [ "postgres" "enable" ] [ "services" "postgres" "enable" ])
  ];



*_Type_*
Expand All @@ -3818,7 +3818,7 @@ boolean


## services.postgres.enable
Whether to enable Add postgreSQL process and psql-devenv script.
Whether to enable Add postgreSQL process script.
.

*_Type_*
Expand Down Expand Up @@ -3862,6 +3862,62 @@ list of strings concatenated with "\n"
```


## services.postgres.initialDatabases
List of database names and their initial schemas that should be used to create databases on the first startup
of Postgres. The schema attribute is optional: If not specified, an empty database is created.


*_Type_*
```
list of (submodule)
```


*_Default_*
```
"[ ]"
```


*_Example_*
```
"[\n {\n name = \"foodatabase\";\n schema = {\n _type = \"literalExpression\";\n text = \"./foodatabase.sql\";\n };\n }\n {\n name = \"bardatabase\";\n }\n]"
```


## services.postgres.initialDatabases.*.name
The name of the database to create.


*_Type_*
```
string
```






## services.postgres.initialDatabases.*.schema
The initial schema of the database; if null (the default),
an empty database is created.


*_Type_*
```
null or path
```


*_Default_*
```
"null"
```




## services.postgres.listen_addresses
Listen address

Expand Down
18 changes: 9 additions & 9 deletions examples/process-compose/devenv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1669076005,
"narHash": "sha256-uzMji2q9Pk3jUH+e5nEFtoOZCP4VV1PDRJRLVmriY0M=",
"lastModified": 1671417167,
"narHash": "sha256-JkHam6WQOwZN1t2C2sbp1TqMv3TVRjzrdoejqfefwrM=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "69335c46c48a73f291d5c6f332fb9fe8b8e22b30",
"rev": "bb31220cca6d044baa6dc2715b07497a2a7c4bc7",
"type": "github"
},
"original": {
Expand All @@ -81,11 +81,11 @@
},
"nixpkgs-stable": {
"locked": {
"lastModified": 1668984258,
"narHash": "sha256-0gDMJ2T3qf58xgcSbYoXiRGUkPWmKyr5C3vcathWhKs=",
"lastModified": 1671271954,
"narHash": "sha256-cSvu+bnvN08sOlTBWbBrKaBHQZq8mvk8bgpt0ZJ2Snc=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "cf63ade6f74bbc9d2a017290f1b2e33e8fbfa70a",
"rev": "d513b448cc2a6da2c8803e3c197c9fc7e67b19e3",
"type": "github"
},
"original": {
Expand All @@ -106,11 +106,11 @@
"nixpkgs-stable": "nixpkgs-stable"
},
"locked": {
"lastModified": 1669152228,
"narHash": "sha256-FEDReoTLWJHXcNso7aaAlAUU7uOqIR6Hc/C/nqlfooE=",
"lastModified": 1671452357,
"narHash": "sha256-HqzXiQEegpRQ4VEl9pEPgHSIxhJrNJ27HfN1wOc7w2E=",
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "078b0dee35e2da01334af682ec347463b70a9986",
"rev": "200790e9c77064c53eaf95805b013d96615ecc27",
"type": "github"
},
"original": {
Expand Down
14 changes: 13 additions & 1 deletion examples/process-compose/devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,28 @@

services.postgres.enable = true;

languages.ruby.enable = true;

packages = [ pkgs.imagemagick6 ];

scripts.compile-rmagick.exec = "gem install --install-dir /tmp rmagick";
thenonameguy marked this conversation as resolved.
Show resolved Hide resolved

processes.bar = {
exec = (pkgs.writeShellScript "complex-process" ''
# testing multiline bash scripts
# testing multiline bash scripts, env vars provided by process-compose
echo "I'm $PC_PROC_NAME, replica: $PC_REPLICA_NUM"

echo
echo "how many files did postgres create?"
ls "$PGDATA" | wc -l

echo
echo 'showing off process-specific env var:'
echo "$BAR"

echo
echo 'can use scripts here as well:'
compile-rmagick
'').outPath;

process-compose = {
Expand Down
29 changes: 18 additions & 11 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,27 @@
in
{
packages = forAllSystems (system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
{
let pkgs = nixpkgs.legacyPackages.${system};
in {
devenv = mkPackage pkgs;
devenv-docs-options = mkDocOptions pkgs;
}
);
});

modules = ./src/modules;

defaultPackage = forAllSystems (system: self.packages.${system}.devenv);

templates.simple = {
path = ./templates/simple;
description = "A direnv supported Nix flake with devenv integration.";
};
templates =
let
simple = {
path = ./templates/simple;
description = "A direnv supported Nix flake with devenv integration.";
};
in
{
inherit simple;
default = simple;
thenonameguy marked this conversation as resolved.
Show resolved Hide resolved
};

lib = {
mkConfig = { pkgs, inputs, modules }:
Expand All @@ -74,7 +78,10 @@
};
modules = [
(self.modules + /top-level.nix)
{ devenv.warnOnNewVersion = false; }
{
packages = [ self.packages.${pkgs.system}.devenv ];
thenonameguy marked this conversation as resolved.
Show resolved Hide resolved
devenv.warnOnNewVersion = false;
}
] ++ modules;
};
in
Expand Down
136 changes: 136 additions & 0 deletions src/modules/mkNakedShell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# 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 DEVENV_PROFILE=${profile}
thenonameguy marked this conversation as resolved.
Show resolved Hide resolved

# 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"
thenonameguy marked this conversation as resolved.
Show resolved Hide resolved

${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
Loading