This commit is contained in:
Burke Libbey 2020-04-28 18:05:04 -04:00
commit 85f8d8dfe5
3 changed files with 121 additions and 0 deletions

44
, Executable file
View File

@ -0,0 +1,44 @@
#!/bin/bash
#
# usage example:
# $ , yarn --help
# This finds a derivation providing a bin/yarn, and runs it with `nix run`.
# If there are multiple candidates, the user chooses one using `fzy`.
set -euo pipefail
if [[ $# -lt 1 ]]; then
>&2 echo "usage: , <program> [arguments]"
exit 1
fi
if [[ "$1" == "--install" ]] || [[ "$1" == "-i" ]]; then
install=1
shift
else
install=""
fi
argv0=$1; shift
case "${argv0}" in
@OVERLAY_PACKAGES@)
attr="${argv0}"
;;
*)
attr="$(nix-locate --db "${NIX_INDEX_DB}" --top-level --minimal --at-root --whole-name "/bin/${argv0}")"
if [[ "$(echo "${attr}" | wc -l)" -ne 1 ]]; then
attr="$(echo "${attr}" | fzy)"
fi
;;
esac
if [[ -z $attr ]]; then
>&2 echo "no match"
exit 1
fi
if [[ -n $install ]]; then
nix-env -iA "nixpkgs.${attr%%.*}"
else
nix run "nixpkgs.${attr}" -c "${argv0}" "$@"
fi

18
README.md Normal file
View File

@ -0,0 +1,18 @@
# ,
Comma runs software without installing it.
Basically it just wraps together `nix run` and `nix-index`. You stick a `,` in front of a command to
run it from whatever location it happens to occupy in `nixpkgs` without really thinking about it.
## Installation
```bash
nix-env -i -f .
```
## Usage
```bash
, cowsay neato
```

59
default.nix Normal file
View File

@ -0,0 +1,59 @@
{ pkgs ? import <nixpkgs> { }
, stdenv ? pkgs.stdenv
, lib ? pkgs.lib
, fetchurl ? pkgs.fetchurl
, nix-index ? pkgs.nix-index
, nix ? pkgs.nix
, fzy ? pkgs.fzy
, makeWrapper ? pkgs.makeWrapper
, runCommand ? pkgs.runCommand
# We use this to add matchers for stuff that's not in upstream nixpkgs, but is
# in our own overlay. No fuzzy matching from multiple options here, it's just:
# Was the command `, mything`? Run `nixpkgs.mything`.
, overlayPackages ? []
}:
let
# nix-index takes a little while to run and the contents don't change
# meaningfully very often. This is generated by running `nix-index` and
# uploading `~/.cache/nix-index/files` to, well, anywhere.
indexCache = fetchurl {
url = "https://s3.amazonaws.com/burkelibbey/nix-index-files";
sha256 = "06p58f82wipd0a8wbc7j3l0p8iaxvdibgshmc9dbxkjf0hmln3kx";
};
# nix-locate needs the --db argument to be a directory containing a file
# named "files".
nixIndexDB = runCommand "nix-index-cache" {} ''
mkdir $out
ln -s ${indexCache.out} $out/files
'';
in
stdenv.mkDerivation rec {
name = "comma";
src = ./.;
buildInputs = [ nix-index.out nix.out fzy.out ];
nativeBuildInputs = [ makeWrapper ];
installPhase = let
caseCondition = lib.concatStringsSep "|" (overlayPackages ++ [ "--placeholder--" ]);
in ''
mkdir -p $out/bin
sed -e 's/@OVERLAY_PACKAGES@/${caseCondition}/' < , > $out/bin/,
chmod +x $out/bin/,
wrapProgram $out/bin/, \
--set NIX_INDEX_DB ${nixIndexDB.out} \
--prefix PATH : ${nix-index.out}/bin \
--prefix PATH : ${nix.out}/bin \
--prefix PATH : ${fzy.out}/bin
ln -s $out/bin/, $out/bin/comma
'';
}