Skip to content

Instantly share code, notes, and snippets.

@manveru
Last active December 14, 2019 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manveru/515ec76ddcecfa21c6b97c344f7bef28 to your computer and use it in GitHub Desktop.
Save manveru/515ec76ddcecfa21c6b97c344f7bef28 to your computer and use it in GitHub Desktop.
Simple wrapper to add packages declaratively
#!/usr/bin/env nix-shell
#!nix-shell -p jq -i bash
# Usage:
#
# nix-install vim
set -e
registry=~/.config/nix/my_packages.json
profile=~/.config/nix/my_profile.nix
pkg="$1"
filter=".packages = (.packages + [\"$pkg\"] | unique)"
if [[ ! $(jq '.packages' "$registry") ]]; then
echo "initializing registry at $registry"
echo '{"packages": []}' > "$registry"
fi
# if [[ ! -f "$profile" ]]; then
#
echo "initializing profile $profile"
cat << NIX > "$profile"
{ pkgs ? import <nixpkgs> {}, name ? "simple-nix" }:
pkgs.buildEnv {
inherit name;
extraOutputsToInstall = ["out" "bin" "lib"];
paths = map (name:
let path = pkgs.lib.splitString "." name;
in pkgs.lib.getAttrFromPath path pkgs
) (
builtins.fromJSON (
builtins.readFile ${registry}
)
).packages;
}
NIX
# fi
echo "adding $pkg to $registry"
updated="$(jq "$filter" "$registry")"
echo "$updated" > "$registry"
nix-env --set -f "$profile" --argstr name "$(whoami)-user-env-$(date -I)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment