From 3baf2453f2e25f608d571a0480b07cda685fed1d Mon Sep 17 00:00:00 2001 From: Julian Mutter Date: Sun, 6 Oct 2024 17:54:46 +0200 Subject: [PATCH] Add conda-direnv package --- packages/conda-direnv/default.nix | 50 +++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 packages/conda-direnv/default.nix diff --git a/packages/conda-direnv/default.nix b/packages/conda-direnv/default.nix new file mode 100644 index 0000000..abbff74 --- /dev/null +++ b/packages/conda-direnv/default.nix @@ -0,0 +1,50 @@ +{ + # Snowfall Lib provides a customized `lib` instance with access to your flake's library + # as well as the libraries available from your flake's inputs. + lib, + # You also have access to your flake's inputs. + inputs, + + # All other arguments come from NixPkgs. You can use `pkgs` to pull packages or helpers + # programmatically or you may add the named attributes as arguments here. + pkgs, + stdenv, + ... +}: + +pkgs.writeShellApplication { + name = "conda-direnv"; + + runtimeInputs = with pkgs; [ + conda + yq + ]; + + text = '' + if [ ! -f environment.yml ]; then + echo "environment.yml does not exist! Exiting..." + exit 1 + fi + + ENV_NAME=$(${pkgs.yq}/bin/yq ".name" environment.yml -r) + if [ "$ENV_NAME" == "" ] || [ "$ENV_NAME" == "null" ]; then + echo "Property \"name\" not in environment.yml or empty! Exiting..." + exit 1 + fi + + echo Welcome to your conda environment "$ENV_NAME"! Activating... + + condanew() { + conda env create -f environment.yml -n $ENV_NAME + conda activate $ENV_NAME + } + + exec ${pkgs.conda}/bin/conda-shell + conda activate $ENV_NAME || condanew + + echo "Environment active! To update it, run:" + echo -e "\tconda env update -f environment.yml -n $ENV_NAME" + + "$SHELL" # needs to be last line so that conda-shell actually enters a shell + ''; +}