]> Git — Sourcephile - sourcephile-nix.git/blob - .envrc
nix: use nixos-unstable-small
[sourcephile-nix.git] / .envrc
1 # manual config
2 nix_version=2.3
3 nix_openpgp=B541D55301270E0BCF15CA5D8170B4726D7198DE
4 nixpkgs_channel=nixos-unstable-small
5 nixshell_sources=(.envrc shell.nix $(test ! -d shell || find shell -type f -not -name "*~" | sort))
6
7 # nix
8 if ! has nix || test "$(nix --version)" != "nix (Nix) $nix_version"
9 then log_status "installing Nix core tools"
10 if test ! -e .config/nix/install -o ! -e .config/nix/install.asc
11 then
12 mkdir -p .config/nix
13 (cd .config/nix; curl -OO https://nixos.org/releases/nix/nix-"$nix_version"/{install,install.asc})
14 gpg2 --keyserver hkp://keys.gnupg.net --recv-keys "$nix_openpgp"
15 fi
16 test -e ~/.nix-profile/etc/profile.d/nix.sh || {
17 gpg --verify .config/nix/install.asc &&
18 sh .config/nix/install
19 }
20 . ~/.nix-profile/etc/profile.d/nix.sh
21 fi
22
23 # nixpkgs
24 if test ! -e .config/nixpkgs-channel/$nixpkgs_channel.nix
25 then log_status "installing nixpkgs from $nixpkgs_channel (This may take some time. To update: delete .config/nixpkgs-channel/$nixpkgs_channel.nix)"
26 rev=$(curl -L https://nixos.org/channels/"$nixpkgs_channel"/git-revision | head -n1 | tr -dC 'a-z0-9')
27 sha256=$(nix-prefetch-url --unpack https://github.com/NixOS/nixpkgs-channels/archive/"$rev".tar.gz)
28 mkdir -p .config/nixpkgs-channel
29 echo >.config/nixpkgs-channel/$nixpkgs_channel.nix "builtins.fetchTarball {url=\"https://github.com/NixOS/nixpkgs-channels/archive/$rev.tar.gz\"; sha256=\"$sha256\";}"
30 else log_status "using nixpkgs from .config/nixpkgs-channel/$nixpkgs_channel.nix"
31 fi
32 watch_file .config/nixpkgs-channel/$nixpkgs_channel.nix
33 # Used in shell.nix
34 export nixpkgs_channel
35 # Get the store path of this nixpkgs,
36 # it will not be registered as a root for the garbage-collector
37 # but will be preserved as long as it is used by shell.nix
38 # which itself will produce a registered derivation.
39 #nixpkgs=$(nix-instantiate --eval .config/nixpkgs-channel/$nixpkgs_channel.nix)
40 #nixpkgs=${nixpkgs#'"'}
41 #nixpkgs=${nixpkgs%'"'}
42 #export NIX_PATH="nixpkgs=$nixpkgs${NIX_PATH:+:$NIX_PATH}"
43
44 # nix-shell
45 has shasum || { log_error "shasum is needed to cache environment"; return 1; }
46 for e in "${nixshell_sources[@]}"
47 do watch_file "$e"; done
48 hash=$(shasum -a 256 "${nixshell_sources[@]}" | shasum -a 256 | cut -c -64)
49 if test -e ".cache/nix-shell/$hash/dump"
50 then
51 log_status "reusing .cache/nix-shell/$hash/"
52 # Load the cached environment
53 direnv_load cat .cache/nix-shell/"$hash"/dump
54 # Re-run the shellHook to update envvars like GPG_TTY,
55 # and run gpg-connect-agent updatestartuptty /bye
56 eval "$shellHook"
57 else log_status "building .cache/nix-shell/$hash/"
58 mkdir -p ".cache/nix-shell/$hash"
59 local dump="$(mktemp .cache/nix-shell/$hash/dump-XXXXXXXX)"
60 # Register the derivation as a root for the garbage-collector,
61 # then cache a dump of the environment from within the nix-shell,
62 # then unregister previous derivations,
63 # then load the cached environment.
64 nix-instantiate >/dev/null ./shell.nix \
65 --indirect --add-root .cache/nix-shell/"$hash"/shell.drv \
66 ${TRACE:+--show-trace} \
67 ${OFFLINE:+--option substituters ""} &&
68 nix-store >/dev/null \
69 --indirect --add-root .cache/nix-shell/"$hash"/shell.dep \
70 ${OFFLINE:+--option substituters ""} \
71 --realise $(nix-store --query --references .cache/nix-shell/"$hash"/shell.drv) &&
72 nix-shell >"$dump" ${TRACE:+--show-trace} --pure \
73 --run "$(join_args "$direnv" dump)" \
74 ${OFFLINE:+--option substituters ""} &&
75 mv -f "$dump" .cache/nix-shell/"$hash"/dump &&
76 find .cache/nix-shell -mindepth 1 -maxdepth 1 -not -name "$hash" -exec rm -rf {} + &&
77 direnv_load cat .cache/nix-shell/"$hash"/dump || {
78 rm -rf "$PWD/.cache/nix-shell/$hash"
79 log_error "cannot build shell.nix"
80 return 1
81 }
82 fi