Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flake templates #188

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/hello-flake/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
6 changes: 6 additions & 0 deletions examples/hello-flake/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "hello-flake"
version = "0.1.0"
edition = "2021"

[dependencies]
27 changes: 27 additions & 0 deletions examples/hello-flake/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
description = "An example devShell with flakes";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
rust-overlay = {
url = "github:oxalica/rust-overlay";
Copy link

@holly-hacker holly-hacker Sep 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For stable toolchains, you can do

-      url = "github:oxalica/rust-overlay";
+      url = "github:oxalica/rust-overlay/stable";

I believe this reduces download size, but haven't checked lately so can't tell you for sure.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, stable branch would result in a smaller download size as the manifests for any nightly versions released since the latest stable would not be included. However, the download size reduction is insignificant enough that it wouldn't be worth increasing the complexity of reading the template for new users.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For stable toolchains, you can do

-      url = "github:oxalica/rust-overlay";
+      url = "github:oxalica/rust-overlay/stable";

I believe this reduces download size, but haven't checked lately so can't tell you for sure.

I didn't explicitly document the stable branch because I have not decided what's the goal for it. Please do not use it for production. Currently, It's just updated less frequently but with the same content (so the same size).

inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, rust-overlay, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs {
inherit system overlays;
};
in
devShells.default = mkShell {
buildInputs = with pkgs; [
rust-bin.stable.latest.default

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a dev shell, wouldnt it be useful to also include rust-analyzer and rust-src?

-            rust-bin.stable.latest.default
+            (rust-bin.stable.latest.default.override {
+              extensions = [ "rust-analyzer" "rust-src" ];
+            })

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure on this one. How useful this would be to users depends on how they have their IDE and system configured. On NixOS this might make more sense. For users on other distributions which use Nix for a specific project, they may have their language servers installed globally (e.g. system package manager, rustup) or by their editor (e.g. VSCode extensions, mason.nvim) which may not pickup an LS in the local environment. Worth including in further discussions about what templates for rust-overlay should be/include.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair, though I'm not sure what the point of a rust devShell would be if rust is already installed globally.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many people prefer using/creating dev shells for open source stuff to lower the barrier of entry to hacking on the project, by removing the need to globally install tools and preventing the dreaded "It works on my machine" problem. They also help ensure the same (versions of) tools are used in development, CI and packaging. Dev shells are still useful for personal projects for these reasons, but their true potential shines when used by multiple people or across many machines.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know what the point of a devShell is 😅 My point was that a devShell isn't needed if rust is already installed globally, especially with rust having almost perfect backwards compatibility (even more so on stable releases as is the case here). This is also a devShell, it doesn't build a package or anything.

I'd agree with you if this was a non-trivial devShell that includes dependencies (eg. pkg-config, openssl, some specific dev tooling, a specific version of the rust toolchain), but this is just a template where people can build upon. People can remove the extensions they don't need after creating it, and it also serves as an example.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this is just a template where people can build upon. People can remove the extensions they don't need after creating it,

No they wouldn't. Most of people will copy code from internet, check if it compiles(evals), and then keep it forever without a second glance. ChatGPT hype and sshd-depends-on-systemd (JiaTan's supply chain attack) all taught us that. I'm against making a "template" too big and/or too complete.

]
}
);
}
3 changes: 3 additions & 0 deletions examples/hello-flake/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
12 changes: 12 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@

# TODO: Flake outputs except `overlay[s]` are not stabilized yet.

templates =
let
hello-flake = {
path = ./examples/hello-flake;
description = "An example devShell with flakes";
};
in
{
default = hello-flake;
inherit hello-flake;
};

packages =
let
select = version: comps:
Expand Down