From 54149dc417819af14ddc0d59216d4add5280ad14 Mon Sep 17 00:00:00 2001 From: DavHau Date: Thu, 17 Feb 2022 14:06:02 +0700 Subject: [PATCH] flake compatibility + index management (#11) * compatibility to flake based systems - add flake.nix - provide interface for updating the index * pin nix to version 2.3 * Update , Co-authored-by: Artturi * remove db outdated error Co-authored-by: Artturi --- , | 15 +++++++++++++-- default.nix | 3 +++ flake.lock | 26 ++++++++++++++++++++++++++ flake.nix | 36 ++++++++++++++++++++++++++++++++++++ update-index.nix | 28 ++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 update-index.nix diff --git a/, b/, index 05d28db..a07cd4d 100755 --- a/, +++ b/, @@ -25,6 +25,9 @@ fi if [[ "$1" == "--install" ]] || [[ "$1" == "-i" ]]; then install=1 shift +elif [[ "$1" == "--update" ]] || [[ "$1" == "-u" ]]; then + ${UPDATE_SCRIPT} + exit else install="" fi @@ -55,8 +58,16 @@ if [[ -z $attr ]]; then exit 1 fi +# on flake based installations nixpkgs is specified via +# flake input and therefore NIX_PATH might be unset +if echo $NIX_PATH | grep -q "nixpkgs="; then + nixArgs="" +else + nixArgs="-I nixpkgs=${NIXPKGS}" +fi + if [[ -n $install ]]; then - nix-env -iA "nixpkgs.${attr%%.*}" + nix-env $nixArgs -iA "nixpkgs.${attr%%.*}" else nix_version_greater_or_equal() { local nix_version @@ -65,7 +76,7 @@ else } if nix_version_greater_or_equal "2.4"; then - nix --extra-experimental-features 'nix-command flakes' shell "nixpkgs#${attr}" -c "${argv0}" "$@" + nix --extra-experimental-features 'nix-command flakes' shell "${NIXPKGS}#${attr}" -c "${argv0}" "$@" else nix run "nixpkgs.${attr}" -c "${argv0}" "$@" fi diff --git a/default.nix b/default.nix index 22670bf..f3d856a 100644 --- a/default.nix +++ b/default.nix @@ -8,6 +8,7 @@ , fzy ? pkgs.fzy , makeWrapper ? pkgs.makeWrapper , runCommand ? pkgs.runCommand +, updateScript ? import ./update-index.nix { inherit pkgs; } , linkFarm ? pkgs.linkFarm # We use this to add matchers for stuff that's not in upstream nixpkgs, but is @@ -48,6 +49,8 @@ stdenv.mkDerivation rec { chmod +x $out/bin/, wrapProgram $out/bin/, \ --set PREBUILT_NIX_INDEX_DB ${nixIndexDB} \ + --set NIXPKGS ${pkgs.path} \ + --set UPDATE_SCRIPT ${updateScript} \ --prefix PATH : ${nix-index}/bin \ --prefix PATH : ${nix}/bin \ --prefix PATH : ${fzy}/bin diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..9439c0f --- /dev/null +++ b/flake.lock @@ -0,0 +1,26 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1638110343, + "narHash": "sha256-hQaow8sGPyUrXgrqgDRsfA+73uR0vms2goTQNxIAaRQ=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "942eb9a335b4cd22fa6a7be31c494e53e76f5637", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "ref": "nixos-unstable", + "type": "indirect" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..effa4af --- /dev/null +++ b/flake.nix @@ -0,0 +1,36 @@ +{ + description = "Comma runs software without installing it"; + + inputs = { + nixpkgs.url = "nixpkgs/nixos-unstable"; + }; + + outputs = { self, nixpkgs, }: + let + b = builtins; + lib = nixpkgs.lib; + supportedSystems = [ "x86_64-linux" "x86_64-darwin" ]; + forAllSystems = f: lib.genAttrs supportedSystems + (system: f system (import nixpkgs { inherit system; })); + in + rec { + + packages = forAllSystems + (system: pkgs: { + comma = import ./default.nix { + inherit pkgs; + updateScript = apps."${system}".update-index.program; + }; + }); + + defaultPackage = forAllSystems (system: pkgs: packages."${system}".comma); + + apps = forAllSystems + (system: pkgs: { + update-index = { + type = "app"; + program = b.toString (pkgs.callPackage ./update-index.nix {}); + }; + }); + }; +} diff --git a/update-index.nix b/update-index.nix new file mode 100644 index 0000000..25cf64c --- /dev/null +++ b/update-index.nix @@ -0,0 +1,28 @@ +{ + pkgs ? import {}, + + coreutils ? pkgs.coreutils, + gnugrep ? pkgs.gnugrep, + lib ? pkgs.lib, + nix-index ? pkgs.nix-index, + writeScript ? pkgs.writeScript, +}: + +writeScript "update-index" '' + PATH=${lib.makeBinPath [ + coreutils + gnugrep + nix-index + ]} + + # on flake based installations nixpkgs is specified via + # flake input and therefore NIX_PATH might be unset + if echo $NIX_PATH | grep -q "nixpkgs="; then + nixpkgs="" + else + nixpkgs="-I nixpkgs=${pkgs.path}" + fi + + mkdir -p $HOME/.cache/comma/ + nix-index -d $HOME/.cache/nix-index -f $nixpkgs +''