Skip to content

Commit a59ac4d

Browse files
authored
Add nix flake to scripts/nix/ (nushell#16848)
I saw nushell#10945 and added some notes in the `scripts/nix/README.md` file on how to use it properly in the non-root directory. Closes nushell#10352. --- Allows including in another flake using the `...?dir=scripts/nix` option on the flake url. Some common uses in the repo: ```console $ # Build nushell $ nix build ./scripts/nix $ # Enter the development shell (sadly it's bash, use .envrc for nu) $ nix develop ./scripts/nix $ # Enable direnv for this project $ "use flake ./scripts/nix" | save -a ./.envrc ``` Everything else should be nicely extracted from the `rust-toolchain.toml` and `Cargo.toml`s around the repo, making changes outside the nix directory require almost no intervention, unless dependencies change or a new plugin is added. --- <!-- Thank you for improving Nushell! Please, read our contributing guide: https://github.com/nushell/nushell/blob/main/CONTRIBUTING.md --> ## Release notes summary - What our users need to know <!-- This section will be included as part of our release notes. See the contributing guide for more details. Please include only details relevant for users here. Motivation and technical details can be added above or below this section. You may leave this section blank until your PR is finalized. Ask a core team member if you need help filling this section. --> ## Tasks after submitting <!-- Remove any tasks which aren't relevant for your PR, or add your own --> - [ ] Update the [documentation](https://github.com/nushell/nushell.github.io)
1 parent 11ae1f2 commit a59ac4d

File tree

5 files changed

+453
-0
lines changed

5 files changed

+453
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,9 @@ tarpaulin-report.html
5454
.direnv/
5555
.envrc
5656

57+
# nix
58+
/result
59+
/scripts/nix/result
60+
5761
# pre-commit-hooks
5862
.pre-commit-config.yaml

scripts/nix/README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Nix Flake for Nushell
2+
3+
Placed in the `scripts/nix` directory to keep it nicely out of the root of the repo, but also allow
4+
including and building with the latest sources. Due to it not being in the root most nix commands
5+
will need to be told the location of the flake:
6+
7+
```console
8+
$ nix run ./scripts/nix
9+
$ nix build ./scripts/nix
10+
$ nix flake update --flake ./scripts/nix
11+
```
12+
13+
## Including in another flake
14+
15+
```nix
16+
# flake.nix
17+
{
18+
inputs = {
19+
nixpkgs.url = "...";
20+
nushell = {
21+
url = "github:nushell/nushell?dir=scripts/nix";
22+
inputs.nixpkgs.follows = "nixpkgs"; # optional
23+
};
24+
};
25+
26+
outputs = inputs: {
27+
# ...
28+
# I would recommend using a `for-each-system` function of some kind here.
29+
packages.x86_64-linux = {
30+
nushell = inputs.nushell.packages.x86_64-linux.nushell;
31+
};
32+
}
33+
}
34+
```
35+
36+
### Adding the hash to `version`
37+
38+
The `inputs.nushell.rev` variable can be used to get the commit hash in the
39+
output of `version`:
40+
41+
```nix
42+
{
43+
# ...
44+
nushell = inputs.nushell.packages.x86_64-linux.nushell.override {
45+
NU_COMMIT_HASH = inputs.nushell.rev;
46+
};
47+
# ...
48+
}
49+
```
50+
51+
## Using the Overlay
52+
53+
This flake also includes an overlay to allow using the new nushell and plugins
54+
without modifying every instance of `nushell` in the configuration. This is a
55+
more complex example than the one above, allowing multiple systems and showing
56+
an example of a `nixosConfiguration` that uses the flakes `nushell`.
57+
58+
```nix
59+
{
60+
outputs = inputs:
61+
let
62+
systems = [
63+
# List of systems to enable, for example:
64+
"x86_64-linux"
65+
"aarch64-darwin"
66+
];
67+
# And for packages or other system-specific inputs, use a function that
68+
# overlays overlays set during the nixpkgs import:
69+
forEachSystem =
70+
f:
71+
inputs.nixpkgs.lib.genAttrs systems (
72+
system:
73+
f {
74+
pkgs = import inputs.nixpkgs {
75+
inherit system;
76+
overlays = [ inputs.nixpkgs.overlays.default ];
77+
}
78+
}
79+
);
80+
in
81+
{
82+
packages = forEachSystem ({pkgs, ...}: {inherit nushell;});
83+
# For a nixos configuration, add it to the `nixpkgs.overlays`. If a nixos
84+
# configuration is all that is required, this is the only part needed
85+
nixosConfiguration.yourHost = inputs.nixpkgs.lib.nixosSystem {
86+
system = "...";
87+
modules = [
88+
{
89+
nixpkgs.overlays = [
90+
inputs.nushell.overlays.default
91+
];
92+
}
93+
./configuration.nix
94+
# ... other modules
95+
];
96+
};
97+
};
98+
}
99+
```
100+
101+
## Using Without Flakes
102+
103+
The package can be built using the `scripts/nix/default.nix` file:
104+
105+
```nix
106+
let
107+
pkgs = import <nixpkgs> { };
108+
in
109+
{
110+
nushell = pkgs.callPackage ./scripts/nix { };
111+
}
112+
```

scripts/nix/default.nix

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Generic builder for nushell and its included plugins.
2+
{
3+
stdenv,
4+
lib,
5+
rustPlatform,
6+
# Allows overriding for the plugins
7+
pname ? "nushell",
8+
# Use this for the cargo file that the plugin has
9+
extraCargo ? ../../Cargo.toml,
10+
withDefaultFeatures ? true,
11+
additionalFeatures ? (p: p),
12+
# Package dependencies
13+
pkg-config,
14+
openssl,
15+
curlMinimal,
16+
python3,
17+
xorg,
18+
nghttp2,
19+
libgit2,
20+
zstd,
21+
zlib,
22+
# Mostly args and things that might be different for plugins
23+
nativeBuildInputs ? [
24+
pkg-config
25+
]
26+
++ lib.optionals (withDefaultFeatures && stdenv.hostPlatform.isLinux) [ python3 ]
27+
++ lib.optionals stdenv.cc.isClang [ rustPlatform.bindgenHook ],
28+
buildInputs ? [
29+
zstd
30+
]
31+
++ lib.optionals stdenv.hostPlatform.isDarwin [ zlib ]
32+
++ lib.optionals (withDefaultFeatures && stdenv.hostPlatform.isLinux) [ xorg.libX11 ]
33+
++ lib.optionals (withDefaultFeatures && stdenv.hostPlatform.isDarwin) [
34+
nghttp2
35+
libgit2
36+
],
37+
checkInputs ?
38+
lib.optionals stdenv.hostPlatform.isDarwin [ curlMinimal ]
39+
++ lib.optionals stdenv.hostPlatform.isLinux [ openssl ],
40+
41+
# Fixed in #16914
42+
buildAndTestSubdir ? ".",
43+
checkFlags ? [ ],
44+
buildType ? "release",
45+
checkType ? buildType,
46+
...
47+
}@args:
48+
let
49+
inherit (builtins)
50+
fromTOML
51+
readFile
52+
head
53+
;
54+
inherit (lib.fileset)
55+
toSource
56+
intersection
57+
unions
58+
difference
59+
;
60+
root = ../..;
61+
cargoToml = fromTOML (readFile (root + /Cargo.toml));
62+
# Allow for extra Cargo.toml files to be passed for crates
63+
extraToml = fromTOML (readFile extraCargo);
64+
in
65+
rustPlatform.buildRustPackage {
66+
inherit
67+
pname
68+
nativeBuildInputs
69+
buildInputs
70+
checkInputs
71+
buildAndTestSubdir
72+
checkFlags
73+
buildType
74+
checkType
75+
;
76+
inherit (cargoToml.package) version;
77+
78+
# Crates will need to build with their Cargo.toml
79+
src = toSource {
80+
inherit root;
81+
fileset = (
82+
intersection root (
83+
difference (unions (
84+
map (p: root + p) [
85+
/Cargo.toml
86+
/Cargo.lock
87+
/src
88+
/crates
89+
/tests
90+
/assets
91+
/benches
92+
/toolkit
93+
/scripts
94+
]
95+
)) ./.
96+
)
97+
);
98+
};
99+
100+
cargoLock = {
101+
lockFile = root + /Cargo.lock;
102+
# Required for some of the dependencies in this repo
103+
allowBuiltinFetchGit = true;
104+
};
105+
106+
buildNoDefaultFeatures = !withDefaultFeatures;
107+
buildFeatures = additionalFeatures [ ];
108+
109+
# Builds the plugins for the test using the right profile, otherwise the
110+
# plugins cannot be found.
111+
NUSHELL_CARGO_PROFILE = checkType;
112+
NU_TEST_LOCALE_OVERRIDE = "en_US.UTF-8";
113+
preCheck = ''
114+
export HOME=$(mktemp -d)
115+
'';
116+
meta = {
117+
description = extraToml.package.description or cargoToml.package.description;
118+
homepage =
119+
if cargoToml == extraToml then cargoToml.package.homepage else extraToml.package.repository or null;
120+
license = lib.licenses.mit;
121+
mainProgram = (head (extraToml.bin or [ extraToml.package ])).name or pname;
122+
};
123+
}

scripts/nix/flake.lock

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)