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 <Artturin@artturin.com>

* remove db outdated error

Co-authored-by: Artturi <Artturin@artturin.com>
This commit is contained in:
DavHau 2022-02-17 14:06:02 +07:00 committed by GitHub
parent 02e3e5545b
commit 54149dc417
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 106 additions and 2 deletions

15
,
View File

@ -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

View File

@ -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

26
flake.lock Normal file
View File

@ -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
}

36
flake.nix Normal file
View File

@ -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 {});
};
});
};
}

28
update-index.nix Normal file
View File

@ -0,0 +1,28 @@
{
pkgs ? import <nixpkgs> {},
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
''