Skip to content

Add a clean nix/nixos development enviroment #163

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Eveeifyeve
Copy link

@Eveeifyeve Eveeifyeve commented Jan 21, 2025

Check description.

Description

Adds a nix/nixos development with automation though github actions with

Motivation and Context

This change is required because there is currently no default dev enviroment for nix/nixos It solves having an environment to use when developing ferrum.

There was previously a pr for adding a flake here, but a follow-up is needed because was a couple problems,

  1. it uses flake utils which is a non module based system which means it makes it harder to make things compatible plus is a non nix way of doing.
  2. building the package inside of the devshell is not really required as the user can run cargo build --release and then use the bin produced.
  3. it's more important to have the cargo lock file checked in for reproducability but also other packages managers and ci for referencing the version and is reccomended by the rust team here even if it's a binary or library.

so this is a followup from #125

How has this been tested?

Local using direnv and nix develop as well as nix flake check plus I have also built in a flake check for each pr that is created.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (restructuring code, without changing its behavior)
  • Add repository changes

Checklist:

  • My code follows the code style of this project.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • Clippy passes with no warnings.

@Eveeifyeve Eveeifyeve force-pushed the nix-flake branch 2 times, most recently from fb7c730 to ba36a01 Compare January 21, 2025 15:25
make flake check action check all systems

added assignment to assign myself to flake update prs.

fix actions to use determinsys

switch nix formatting check to flake check

fix formatting for nix
@nicholaschiasson
Copy link

I think you got things backwards with the Cargo.lock. You want to check your Cargo.lock into version control for binaries, not libraries.

@Eveeifyeve
Copy link
Author

Eveeifyeve commented Jan 24, 2025

I think you got things backwards with the Cargo.lock. You want to check your Cargo.lock into version control for binaries, not libraries.

No I think it's okay to have it in version control even if it's a binary & libraries see more at https://doc.rust-lang.org/cargo/faq.html#why-have-cargolock-in-version-control

@nicholaschiasson
Copy link

nicholaschiasson commented Jan 25, 2025

No I think it's okay to have it in version control even if it's a binary & libraries see more at https://doc.rust-lang.org/cargo/faq.html#why-have-cargolock-in-version-control

Indeed, maybe to rephrase: it is more important, if anything, to check in the Cargo.lock for binaries, than for libraries. I was pointing it out because you stated in the PR description the opposite.

Also, and you're free to disagree on this of course, but I would have thought the Cargo.lock would be important for a nix setup (flake or not) given the mission statement of nix/nixpkgs/nixos being to guarantee reproducibility. For this reason, I kind of thought you would want the dependency versions pinned if it makes sense to do so (which it does since this repo outputs a binary). I'm definitely not a nix expert though so please feel free to share your thoughts on this.

@Eveeifyeve
Copy link
Author

Eveeifyeve commented Jan 25, 2025

No I think it's okay to have it in version control even if it's a binary & libraries see more at https://doc.rust-lang.org/cargo/faq.html#why-have-cargolock-in-version-control

Indeed, maybe to rephrase: it is more important, if anything, to check in the Cargo.lock for binaries, than for libraries. I was pointing it out because you stated in the PR description the opposite.

I will rephase as it's my fault originally as I was rushing this pr before I go to asleep.

Also, and you're free to disagree on this of course, but I would have thought the Cargo.lock would be important for a nix setup (flake or not) given the mission statement of nix/nixpkgs/nixos being to guarantee reproducibility. For this reason, I kind of thought you would want the dependency versions pinned if it makes sense to do so (which it does since this repo outputs a binary). I'm definitely not a nix expert though so please feel free to share your thoughts on this.

Yes, but when you use release tags the versions are pinned by github.
I also forgot to enable it.

@Sweattypalms
Copy link
Member

Is this completed?

@Eveeifyeve
Copy link
Author

Is this completed?

Almost, Just need to fix the github action caches then it's ready to merge.

@Eveeifyeve
Copy link
Author

Eveeifyeve commented Feb 2, 2025

@Sweattypalms Okay i've done all of my changes, Ready to merge if you wanted to.

Choose a reason for hiding this comment

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

diff --git a/flake.nix b/flake.nix
index cd51e48..4c69f92 100644
--- a/flake.nix
+++ b/flake.nix
@@ -1,10 +1,15 @@
 {
   inputs = {
     nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
-    flake-parts.url = "github:hercules-ci/flake-parts";
-    flake-compat = {
-      url = "github:edolstra/flake-compat";
-      flake = false;
+    systems.url = "github:nix-systems/default";
+    flake-compat.url = "github:edolstra/flake-compat";
+    treefmt-nix = {
+      url = "github:numtide/treefmt-nix";
+      inputs.nixpkgs.follows = "nixpkgs";
+    };
+    flake-parts = {
+      url = "github:hercules-ci/flake-parts";
+      inputs.nixpkgs-lib.follows = "nixpkgs";
     };
     rust-overlay = {
       url = "github:oxalica/rust-overlay";
@@ -14,13 +19,19 @@
 
   outputs =
     inputs@{
+      systems,
       flake-parts,
+      treefmt-nix,
       nixpkgs,
       rust-overlay,
       ...
     }:
     flake-parts.lib.mkFlake { inherit inputs; } {
-      systems = nixpkgs.lib.systems.flakeExposed;
+      systems = import systems;
+
+      imports = [
+        treefmt-nix.flakeModule
+      ];
 
       perSystem =
         {
@@ -28,51 +39,27 @@
           system,
           ...
         }:
+        let
+          rustToolchain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
+        in
         {
-          formatter = nixpkgs.legacyPackages.${system}.nixfmt-rfc-style;
           _module.args.pkgs = import inputs.nixpkgs {
             inherit system;
-            overlays = [
-              rust-overlay.overlays.default
-              (self: super: {
-                rustToolchain =
-                  let
-                    rust = super.rust-bin;
-                  in
-                  if builtins.pathExists ./rust-toolchain.toml then
-                    rust.fromRustupToolchainFile ./rust-toolchain.toml
-                  else if builtins.pathExists ./rust-toolchain then
-                    rust.fromRustupToolchainFile ./rust-toolchain
-                  else
-                    rust.nightly.latest.default;
-              })
-            ];
-            config = { };
+            overlays = [ rust-overlay.overlays.default ];
           };
 
-          # Used to check formatting for nix specificly
-          checks.fmt-check =
-            pkgs.runCommand "format-check"
-              {
-                src = ./.;
-                doCheck = true;
-                nativeBuildInputs = [
-                  pkgs.nixfmt-rfc-style
-                ];
-              }
-              ''
-                					nixfmt --check .
-                					touch $out
-                				'';
+          treefmt = {
+            projectRootFile = "flake.lock";
+            programs.nixfmt.enable = true;
+          };
 
           devShells.default = pkgs.mkShell {
-            packages = with pkgs; [
+            packages = [
               rustToolchain
-              pkg-config
-              openssl
+              pkgs.pkg-config
+              pkgs.openssl
             ];
           };
-
         };
     };
 }

Choose a reason for hiding this comment

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

  • I suggest to use numtide/treefmt-nix to create a formatter.
  • I also suggest to use nix-systems/default to reference available systems.

Copy link
Author

Choose a reason for hiding this comment

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

Why use nix-systems can I ask because nixpkgs.lib.systems.flakeExposed does basically the same thing but with an extra dependancy.

Choose a reason for hiding this comment

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

<nixpkgs>/lib/systems/flake-systems.nix
This file contains not only x86_64-linux and aarch64-linux, but also i686-linux, powerpc64le-linux and riscv64-linux. Are we even needed to support powerpc64le-linux & riscv64-linux systems?

The nix-systems/default contains only these systems. If we don't support Darwin systems, we can use nix-systems/default-linux.

# github:nix-systems/default

[
  "aarch64-darwin"
  "aarch64-linux"
  "x86_64-darwin"
  "x86_64-linux"
]
# github:nix-systems/default-linux

[
  "aarch64-linux"
  "x86_64-linux"
]

Copy link
Collaborator

Choose a reason for hiding this comment

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

We want support for windows, linux and mac, with linux and mac being able to work on ARM64 systems. I have no idea how nix works so if you guys can make that work and are happy to maintain it, I'll merge it.

Copy link
Collaborator

@ReCore-sys ReCore-sys May 12, 2025

Choose a reason for hiding this comment

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

Yeah I wasn't saying I wanted nix on windows, I just meant that we'd like to support those 3 platforms along with arm versions for linux and mac. If you wanna make nix setups for as many as you can and you are going to be able to maintain those, works for me.

Copy link
Author

@Eveeifyeve Eveeifyeve May 12, 2025

Choose a reason for hiding this comment

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

There is still a lot to go with nix on windows, please check following issues if you want to help contribute nix on windows https://github.com/NixOS/nix/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20label%3Awindows and here is following matrix chat where you can get help with https://matrix.to/#/#windows:nixos.org

Choose a reason for hiding this comment

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

OK, I understood. Yeah @haruki7049 (and maybe also @Eveeifyeve) will maintain the Nix code. Just ping to us when a problem related by any Nix code.

The flake.nix helps Nix users to develop/try ferrumc. I'm glad to have find this PR, because I'm heavy Nix/NixOS user.

Copy link
Author

Choose a reason for hiding this comment

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

There is a working fork but that is decades old https://github.com/nix-windows/nix and not to mention the 120k packages that you would have to fix in nixpkgs to fix that is way too much for supporting windows

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sure. You guys see if there are any final changes and when it's ready, lmk and ill merge it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants