Giter Site home page Giter Site logo

srid / rust-nix-template Goto Github PK

View Code? Open in Web Editor NEW
183.0 3.0 18.0 77 KB

Rust project template with Nix (Flakes) and VSCode support

Home Page: https://srid.ca/rust-nix

License: MIT License

Nix 70.53% Rust 19.26% Just 10.20%
rust nix template-project vscode

rust-nix-template's Introduction

A template Rust project with fully functional and no-frills Nix support, as well as builtin VSCode configuration to get IDE experience without any manual setup (just install direnv, open in VSCode and accept the suggestions). It uses crane, via rust-flake.

Note

If you are looking for the original template based on this blog post's use of crate2nix, browse from this tag. The evolution of this template can be gleaned from releases.

Adapting this template

  • Run nix develop to have a working shell ready before name change.
  • Change name in Cargo.toml.
  • Run cargo generate-lockfile in the nix shell
  • There are two CI workflows, and one of them uses Nix which is slower (unless you configure a cache) than the other that is based on rustup. Pick one or the other depending on your trade-offs.

Development (Flakes)

This repo uses Flakes from the get-go.

# Dev shell
nix develop

# or run via cargo
nix develop -c cargo run

# build
nix build

We also provide a justfile for Makefile'esque commands.

Discussion

See Also

rust-nix-template's People

Contributors

dependabot[bot] avatar firefragment avatar imcvampire avatar j2ghz avatar srid avatar sureyeaah avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rust-nix-template's Issues

Direnv not working

Using direnv doesn't seem to start the shell but nix develop does.

direnv: loading ~/Projects/rust/rust-nix-template/.envrc
direnv: using flake
direnv: nix-direnv: using cached dev shell

### ️🔨 Welcome to the Nix devshell ###

Available commands:

## Commands

  , fmt    : Auto-format project tree
  , run    : Run the project executable
  , watch  : Watch for changes and run the project executable
/nix/store/09g3fqxa257s69yr080a989i2bham1zj-nix-direnv-2.3.0/share/nix-direnv/direnvrc:238: pop_var_context: head of shell_variables not a function context
/nix/store/09g3fqxa257s69yr080a989i2bham1zj-nix-direnv-2.3.0/share/nix-direnv/direnvrc:1073: pop_var_context: head of shell_variables not a function context
environment:1: pop_var_context: head of shell_variables not a function context
direnv: error exit status 1

I found some information about how it stems from bash in nixpkgs-unstable but switching to 22.11 or 23.05 didn't help so unclear.

Hover not working

Hover over even standard library functions are not working in VSCode.

Unable to compile Reqwest via nix-shell.

I've a simple project where I'm using reqwest and ring.

Cargo.toml

[dependencies]
reqwest = { version = "0.11.24", features = ["blocking", "rustls-tls"], default-features = false }
ring = "0.17.8"

main.rs

fn main(){
     let client = reqwest::blocking::Client::new();
    dbg!(client);
}

I do have ld but the issue still persists, Do you have any suggestion? Can understand it doesn't relate to the flake itself.

Screenshot 2024-03-03 at 7 14 37 AM

buildRustCrate is deprecated

When cloning the repo and trying to run nix run it errors with the following.

trace: warning: `buildRustCrate` is deprecated, use `buildRustCrateForPkgs` instead
trace: warning: `buildRustCrate` is deprecated, use `buildRustCrateForPkgs` instead
error: store path '/nix/store/v3svm8vpdb16pd1iblkh3dg4rhg8c0wq-rust-nix-template-crate2nix' is not allowed to have references

There's a section in the crate2nix FAQ about this.
https://github.com/kolloch/crate2nix#i-get-a-warning-about-buildrustcrate-being-deprecated-in-favor-of-buildrustcrateforpkgs

Fails to build when using packages from git in Cargo.toml

I replaced the line in the example Cargo.toml: clap = "23.2.1" to clap = { git="https://github.com/clap-rs/clap" } and it fails to build.
When getting the nix log of nix develop:

@nix { "action": "setPhase", "phase": "unpackPhase" }
unpacking sources
unpacking source archive /nix/store/ayx76ymm5r1808sl2dyzpbs9jpsshv3m-zp6ag6v7ilzhs2gmpyqr279rml3d78ki-source
source root is zp6ag6v7ilzhs2gmpyqr279rml3d78ki-source
@nix { "action": "setPhase", "phase": "buildPhase" }
building
++ crate2nix generate -f ./Cargo.toml -o Cargo-generated.nix -h /nix/store/s3n05229i2z88ing067ys1hr3kmq00vq-rust-nix-template-crate2nix/crate-hashes.json
Error: while retrieving metadata about ./Cargo.toml: Error during execution of `cargo metadata`: error: could not load Cargo configuration

Caused by:
  could not parse TOML configuration in `/nix/store/s3n05229i2z88ing067ys1hr3kmq00vq-rust-nix-template-crate2nix/cargo/config`

Caused by:
  could not parse input as TOML

Caused by:
  redefinition of table `source.https://github.com/clap-rs/clap` for key `source.https://github.com/clap-rs/clap` at line 10 column 1

crate2nix failed.
== cargo/config (BEGIN)
    [source.crates-io]
    replace-with = "vendored-sources"
    
    [source."https://github.com/clap-rs/clap"]
    git = "https://github.com/clap-rs/clap"
    rev = "a11ca85e8fd401e07c8330980d0af090a3d35d03"
   branch = "master"
    replace-with = "vendored-sources"
    
    [source."https://github.com/clap-rs/clap"]
    git = "https://github.com/clap-rs/clap"
    rev = "a11ca85e8fd401e07c8330980d0af090a3d35d03"
    branch = "master"
    replace-with = "vendored-sources"
    
    
    [source.vendored-sources]
    directory = "/nix/store/z6w7g6izihmyvkphphsyp0hxn6zz26v9-deps"

== cargo/config (END)

== crate-hashes.json (BEGIN)
    {"clap 3.0.0-beta.2 (git+https://github.com/clap-rs/clap#a11ca85e8fd401e07c8330980d0af090a3d35d03)":"d57b43b2572f4ec4fd5f2b33a05d5189d216cba28e53a0d9b6826ee4d2ca5429","clap_derive 3.0.0-beta.2 (git+https://github.com/clap-rs/clap#a11ca85e8fd401e07c8330980d0af090a3d35d03)":"d57b43b2572f4ec4fd5f2b33a05d5189d216cba28e53a0d9b6826ee4d2ca5429"}
== crate-hashes.json (END)

== ls -la (BEGIN)
total 60
drwxr-xr-x 6 nixbld1 nixbld 4096 Jan  1  1970 .
drwx------ 3 nixbld1 nixbld 4096 Jul  7 19:05 ..
drwxr-xr-x 3 nixbld1 nixbld 4096 Jan  1  1970 .github
-rw-r--r-- 1 nixbld1 nixbld   28 Jan  1  1970 .gitignore
drwxr-xr-x 2 nixbld1 nixbld 4096 Jan  1  1970 .vscode
-rw-r--r-- 1 nixbld1 nixbld 6613 Jan  1  1970 Cargo.lock
-rw-r--r-- 1 nixbld1 nixbld  347 Jan  1  1970 Cargo.toml
-rw-r--r-- 1 nixbld1 nixbld 1151 Jan  1  1970 README.md
drwxr-xr-x 2 nixbld1 nixbld 4096 Jan  1  1970 bin
-rw-r--r-- 1 nixbld1 nixbld  320 Jan  1  1970 default.nix
-rw-r--r-- 1 nixbld1 nixbld 3349 Jan  1  1970 flake.lock
-rw-r--r-- 1 nixbld1 nixbld 3331 Jan  1  1970 flake.nix
-rw-r--r-- 1 nixbld1 nixbld  318 Jan  1  1970 shell.nix
drwxr-xr-x 2 nixbld1 nixbld 4096 Jan  1  1970 src

I'm also getting a deprecation warning about buildRustCrate

trace: warning: `buildRustCrate` is deprecated, use `buildRustCrateForPkgs` instead

Regarding `RUST_SRC_PATH`

# FIXME: Is this correct? Should it use rust-overlay instead?
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";

This would work in most cases, but ideally, the rust-src component is on par with the active rustc toolchain (that is how rustup does it).

I source the rust-src component from mozillapkgs and export RUST_SRC_PATH like so (I think oxalica/rust-overlay also exposes the same rust-src component through rust-bin.<channel>.latest.rust-src):

RUST_SRC_PATH = "${rust-src}/lib/rustlib/src/rust/library";

RUST_SRC_PATH should contain the following:

alloc/        panic_unwind/       rustc-std-workspace-alloc/  stdarch/
backtrace/    proc_macro/         rustc-std-workspace-core/   term/
core/         profiler_builtins/  rustc-std-workspace-std/    test/
panic_abort/  rtstartup/          std/                        unwind/

There has been some discussion about providing rust-src from the nix store and rust-analyzer (rust-analyzer tries to write a Cargo.lock to the nix store and fails) but I have had no trouble with this.

I use this in my rust + nix flake setup (which has been modified heavily based on your template, thanks for that).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.