Skip to content

Commit

Permalink
nix: Fix Nix build, add more Nix usage docs, more relaxed Rust build …
Browse files Browse the repository at this point in the history
…artifacts discovery (#28)

* docs(nix): more usage info

* feat(nix): build Rust workspace with crane

* fix: even more relaxed sg-cody and libsg_nvim discovery

* feat(nix): .envrc for nix-direnv ergonomics

* fix(nix): plugin derivation should not trigger `make`

* ref+bump: cleaner syntax from flakes-part and bumpb nixpkgs

* fix: add PATH onto sg-cody vim.loop.spawn, solves the case cmd is 'sg-cody'

* fix(ci): update cachix/install-nix-action
- ref: cachix/install-nix-action#183

* ref: move .nix artifacts to contrib/

* bump deps
  • Loading branch information
Pegasust authored Jun 28, 2023
1 parent f1fa0cf commit 3574508
Show file tree
Hide file tree
Showing 13 changed files with 395 additions and 98 deletions.
6 changes: 6 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# If nix-shell available, then nix is installed. We're going to use nix-direnv.
# for automatic devshell injection after opt-in `direnv allow`
if command -v nix-shell &> /dev/null
then
use flake
fi
2 changes: 1 addition & 1 deletion .github/workflows/nix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
name: nix-build (${{ matrix.os }})
steps:
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v20
- uses: cachix/install-nix-action@v22
with:
github_access_token: ${{ secrets.GITHUB_TOKEN }}
- run: nix build
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
.direnv
.envrc
.env
.pre-commit-config.yaml
# nix build artifact
result

### Rust ###
debug/
Expand Down
37 changes: 28 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,34 @@ The project is packaged as a [Nix Flake][nix-flakes]. Consume it as you normally
*make sure* that sg-nvim is included *both* as a Neovim plugin *and* as an environment/user package
(because `sg-lsp` needs to be on your PATH).

See https://nixos.wiki/wiki/Neovim for more details on configuring neovim using Nix.
Or see https://github.com/willruggiano/neovim.drv for a practical example.

For contributors and maintainers:

- There should be nothing to do, nix-related, when changes are made to the Rust project
- If you're Nix savvy and want to contribute, it would be nice to use [crate2nix] instead
of the generic `buildRustPackage`. A github workflow would be needed to autoupdate the
generated crate2nix files.
See [Neovim guide on NixOS wiki](https://nixos.wiki/wiki/Neovim) for more details on configuration
See [gh:willruggiano/neovim.drv](https://github.com/willruggiano/neovim.drv) for a practical configuration.

For Nix contributors and maintainers:

- Feel free to `nix flake update` every once in a while to make sure `flake.lock` is up-to-date
- [ ] Minimal `sg.nvim`-integrated neovim package for testing and example
- [ ] Integrate `sg.nvim` + Cody onto [nixpkgs:vimPlugins](https://github.com/NixOS/nixpkgs/tree/fe2fb24a00ec510d29ccd4e36af72a0c55d81ec0/pkgs/applications/editors/vim/plugins)

You will also need to add the built `.cdylib` onto `package.cpath`. Here is one example
using [gh:willruggiano/neovim.nix](https://github.com/willruggiano/neovim.nix):

```nix
sg = let
system = "x86_64-linux";
package = inputs.sg-nvim.packages.${system}.default;
in {
inherit package;
init = pkgs.writeTextFile {
name = "sg.lua";
text = ''
return function()
package.cpath = package.cpath .. ";" .. "${package}/lib/?.so;${package}/lib/?.dylib"
end
'';
};
};
```

### Setup:

Expand Down
13 changes: 13 additions & 0 deletions contrib/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
pkgs,
symlinkJoin,
sg-workspace ? (pkgs.callPackage (import ./workspace-drv.nix)),
sg-plugin ? (pkgs.callPackage (import ./plugin-drv.nix)),
meta ? (pkgs.callPackage (import ./meta.nix)),
...
}:
symlinkJoin {
name = "sg.nvim";
paths = [sg-workspace sg-plugin];
inherit meta;
}
5 changes: 5 additions & 0 deletions contrib/meta.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{lib, ...}: {
description = "A plugin focused on bringing many features of sourcegraph.com onto Neovim";
homepage = "https://github.com/sourcegraph/sg.nvim";
license = lib.licenses.unlicense;
}
17 changes: 17 additions & 0 deletions contrib/plugin-drv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
pkgs,
stdenv,
proj_root ? ./.,
meta ? (pkgs.callPackage (import ./meta.nix)),
...
}:
stdenv.mkDerivation {
name = "sg.nvim-plugin";
src = proj_root;
phases = ["installPhase"];
installPhase = ''
mkdir -p $out
cp -r $src/{lua,plugin} $out
'';
inherit meta;
}
74 changes: 74 additions & 0 deletions contrib/workspace-drv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
pkgs,
lib,
craneLib, # TODO: Make this flakes-free
pkg-config,
openssl,
stdenv,
darwin,
proj_root ? ./.,
meta ? (pkgs.callPackage (import ./meta.nix)),
...
}: let
# PURPOSE: if you modify non-code like github workflows, nix should not trigger a rebuild.
# cleanCargoSource keeps only `.rs`, `.toml`, and files listed below as build source
# NOTE: if you use `include!(<file>)` in Rust code. You'll have to opt-in the file
# using a custom filter. See https://github.com/ipetkov/crane/blob/master/lib/filterCargoSources.nix
code_artifacts =
lib.cleanSourceWith
{
src = lib.cleanSource proj_root;
filter = orig_path: type: let
path = toString orig_path;
base = baseNameOf path;
parentDir = baseNameOf (dirOf path);

matchesSuffix = lib.any (suffix: lib.hasSuffix suffix base) [
# Keep rust sources
".rs"

# Rust configs
"Cargo.toml"
"config.toml"

".gql"
".graphql"
];

# Cargo.toml already captured above
isCargoFile = base == "Cargo.lock";

# .cargo/config.toml already captured above
isCargoConfig = parentDir == ".cargo" && base == "config";
in
type == "directory" || matchesSuffix || isCargoFile || isCargoConfig;
};

crane-args = {
pname = "sg.nvim-workspace";
version = (with builtins; fromTOML (readFile "${proj_root}/Cargo.toml")).package.version or "unknown";
src = code_artifacts;

nativeBuildInputs = [pkg-config];

# openssl: required by reqwest (-> hyper-tls -> native-tls)
buildInputs =
[openssl]
++ (lib.optional stdenv.isDarwin [
darwin.apple_sdk.frameworks.Security
]);

cargoExtraArgs = "--workspace";
inherit meta;
};

# build a version with only deps to reuse as build cache
workspace-deps = craneLib.buildDepsOnly crane-args;

workspace-all = craneLib.buildPackage (crane-args
// {
# PURPOSE: This attempts to reuse build cache to skip having to build dependencies
cargoArtifacts = workspace-deps;
});
in
workspace-all
40 changes: 0 additions & 40 deletions default.nix

This file was deleted.

Loading

0 comments on commit 3574508

Please sign in to comment.