This repository has been archived by the owner on Oct 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
shell.nix
199 lines (161 loc) · 5.27 KB
/
shell.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
{
# Pull in tools & environment variables that are only
# required for interactive development (i.e. not necessary
# on CI). Only when this is enabled, Rust nightly is used.
isDevelopmentShell ? true
, nixpkgs ? ./nix/nixpkgs-stable.nix
, pkgs ? import nixpkgs
}:
let
# Keep project-specific shell commands local
HISTFILE = "${toString ./.}/.bash_history";
# Lorri-specific
# The root directory of this project
LORRI_ROOT = toString ./.;
# Needed by the lorri build.rs to determine its own version
# for the development repository (non-release), we set it to 1
BUILD_REV_COUNT = 1;
# Needed by the lorri build.rs to access some tools used during
# the build of lorri's environment derivations.
RUN_TIME_CLOSURE = pkgs.callPackage ./nix/runtime.nix {};
# Rust-specific
# Enable printing backtraces for rust binaries
RUST_BACKTRACE = 1;
# Only in development shell
# Needed for racer “jump to definition” editor support
# In Emacs with `racer-mode`, you need to set
# `racer-rust-src-path` to `nil` for it to pick
# up the environment variable with `direnv`.
RUST_SRC_PATH = "${pkgs.rustc.src}/lib/rustlib/src/rust/src/";
# Set up a local directory to install binaries in
CARGO_INSTALL_ROOT = "${LORRI_ROOT}/.cargo";
buildInputs = [
pkgs.cargo
pkgs.rustPackages.clippy
pkgs.rustc
pkgs.rustfmt
pkgs.git
pkgs.direnv
pkgs.shellcheck
pkgs.carnix
pkgs.nix-prefetch-git
pkgs.nixpkgs-fmt
# To ensure we always have a compatible nix in our shells.
# CI doesn’t know `nix-env` otherwise.
pkgs.nix
] ++ pkgs.stdenv.lib.optionals pkgs.stdenv.isDarwin [
pkgs.darwin.Security
pkgs.darwin.apple_sdk.frameworks.CoreServices
pkgs.darwin.apple_sdk.frameworks.CoreFoundation
];
in
pkgs.mkShell (
{
name = "lorri";
buildInputs = buildInputs
++ pkgs.stdenv.lib.optionals isDevelopmentShell [ pkgs.rustracer ];
inherit BUILD_REV_COUNT RUN_TIME_CLOSURE;
inherit RUST_BACKTRACE;
# Executed when entering `nix-shell`
shellHook = ''
# we can only output to stderr in the shellHook,
# otherwise direnv `use nix` does not work.
# see https://github.com/direnv/direnv/issues/427
exec 3>&1 # store stdout (1) in fd 3
exec 1>&2 # make stdout (1) an alias for stderr (2)
alias ci="ci_check"
# this is mirrored from .envrc to make available from nix-shell
# pick up cargo plugins
export PATH="$LORRI_ROOT/.cargo/bin:$PATH"
# watch the output to add lorri once it's built
export PATH="$LORRI_ROOT/target/debug:$PATH"
function ci_lint() (
cd "$LORRI_ROOT";
set -x
${import ./nix/mdoc-lint.nix { inherit pkgs; } ./lorri.1}
manpage=$?
./nix/fmt.sh --check
nix_fmt=$?
${import ./.github/workflows/ci.nix { inherit pkgs; }}
ciwrite=$?
git diff --quiet -- ./.github/workflows/ci.yml
cidiff=$?
ciupdate=$((ciwrite + cidiff))
./nix/update-carnix.sh
carnixupdate=$?
git diff --quiet -- Cargo.nix
carnixdiff=$?
carnixupdate=$((carnixupdate + carnixdiff))
cargo fmt -- --check
cargofmtexit=$?
RUSTFLAGS='-D warnings' cargo clippy
cargoclippyexit=$?
set +x
echo "./nix/fmt.sh --check: $nix_fmt"
echo "carnix update: $carnixupdates"
echo "Cargo.nix changed: $carnixdiff"
echo "cargo fmt: $cargofmtexit"
echo "cargo clippy: $cargoclippyexit"
sum=$((manpage + nix_fmt + ciupdate + carnixupdate + cargofmtexit + cargoclippyexit))
if [ "$sum" -gt 0 ]; then
return 1
fi
)
function ci_test() (
cd "$LORRI_ROOT";
set -x
./script-tests/run-all.sh
scripttests=$?
cargo test
cargotestexit=$?
git diff --quiet -- src/
gendiff=$?
set +x
echo "script tests: $scripttests"
echo "generated files changed: $gendiff"
echo "cargo test: $cargotestexit"
sum=$((scripttest + cargotestexit + gendiff))
if [ "$sum" -gt 0 ]; then
return 1
fi
)
function ci_check() (
ci_lint
lint=$?
ci_test
test=$?
sum=$((lint + test))
if [ "$sum" -gt 0 ]; then
return 1
fi
)
${pkgs.lib.optionalString isDevelopmentShell ''
echo "lorri" | ${pkgs.figlet}/bin/figlet | ${pkgs.lolcat}/bin/lolcat
(
format=" %-12s %s\n"
printf "$format" alias executes
printf "$format" ----- --------
IFS=$'\n'
for line in $(alias); do
[[ $line =~ ^alias\ ([^=]+)=(\'.*\') ]]
printf "$format" "''${BASH_REMATCH[1]}" "''${BASH_REMATCH[2]}"
done
)
''}
# restore stdout and close 3
exec 1>&3-
'' + (
if !pkgs.stdenv.isDarwin then "" else ''
# Cargo wasn't able to find CF during a `cargo test` run on Darwin.
export NIX_LDFLAGS="-F${pkgs.darwin.apple_sdk.frameworks.CoreFoundation}/Library/Frameworks -framework CoreFoundation $NIX_LDFLAGS"
''
);
preferLocalBuild = true;
allowSubstitutes = false;
}
// (
if isDevelopmentShell then {
inherit RUST_SRC_PATH CARGO_INSTALL_ROOT HISTFILE;
} else {}
)
)