this post was submitted on 10 May 2024
14 points (100.0% liked)

Nix / NixOS

1758 readers
56 users here now

Main links

Videos

founded 1 year ago
MODERATORS
 

I'm very, very new to nix and nixOS both - I come from imperative workflows and very very rarely anything determinative, so this is all brand new to me.

As an example of the kind of thing I don't understand how to do, let's take an example repo I've been bouncing off: https://github.com/GideonWolfe/Chameleon

On a "normal" system, I can get pip and python ready, and then make install and I'm off to the races.

With NixOS, I've got as far as adding python3 and gnumake to my configuration.nix packages. (I have also discovered that putting python in my system packages was the wrong move, so some advice on how better to go about this would be cool too.)

I can't for the life of me wrap my head around what I'm supposed to do, and so many people online are using flakes but I'm on stable 23.11 (and quite daunted by flakes) so I'd prefer if this was from that POV.

Can anyone speak to any of these points? I've tried reading the docs but it's very confusing for some reason.

top 13 comments
sorted by: hot top controversial new old
[–] onlinepersona@programming.dev 11 points 6 months ago (2 children)

Here are the basic steps though:

  • package the target in a derivation aka a recipe to package the thing you like
  • test it in a nix-shell or add it straight to your configuration.nix

I know it sounds a lot like , but a few pointers:

Derivation basics: This wiki page is the one that helped me understand the basics of derivations. It explains how to package stuff without extra tooling. stdenv.mkDerivation from nixpkgs adds a bunch of stuff, but the wiki linked doesn't explain it (yet?). You might find more information elsewhere.

There's documentation for stdenv.mkDerivation and I apologize in advance for putting this evil upon you, but right now I don't have any other useful bookmarks. I learned it the hard way, but if you can contribute to the https://nixlang.wiki with what you learned, you could make it easier for the next person.

As for nix-shell, once you've written your default.nix, you can wrap it in a shell.nix and execute nix-shell in the same directory.

default.nix

{ pkgs ? import <nixpkgs> {} }:
pkgs.stdenv.mkDerivation {
  pname = "chameleon";
  version = "0.0.1"; # Update this to the real version of the package
  buildInputs = with pkgs; [ gnumake ]; # Add more necessary inputs here

  # While testing, your default.nix can reside in the checked out git repo
  src = ./.;
  # You can of course also start out like this straight away, up to you
  # Once testing is done, you can point this to a git repo pulled by nix
  # src = pkgs.fetchFromGitHub {
  #   owner = "GideonWolfe";
  #   repo = "Chameleon";
  #   rev = "SOME GIT REVISION";
  #   hash = "";  # Start with an empty string and let nix complain with the real one, the use it
  #  };

  # There's a chance an install phase isn't necessary, but this is a skeleton
  # The contents are run in bash
  installPhase = ''
   runHook preInstall
   
   # Do non-standard installation stuff
   # See https://nixos.org/manual/nixpkgs/stable/#ssec-install-phase
   # for what is done by default

   runHook postInstall
  '';
}

shell.nix

{ pkgs ? import <nixpkgs> {} }:
let
  my-package = (import ./default.nix) { pkgs = pkgs; };
in
pkgs.mkShell {
  inputsFrom = [ my-package ];
}

Then you can run nix-shell and see if your expected binary is in PATH. Once that is confirmed, you can add your package to your configuration.nix

{ config, pkgs, ... }:

{
  # The rest of your configuration.nix

  environment.systemPackages = with pkgs; [
    # Import and call your derivation
    ((import /path/to/your/default.nix) { inherit pkgs; })
  ];
}

Hopefully that helped a little.

Anti Commercial-AI license

[–] apotheotic@beehaw.org 3 points 6 months ago

Thank you deeply for such a thorough answer! If I make any discoveries that aren't documented I will definitely try to contribute to the wiki.

[–] apotheotic@beehaw.org 1 points 6 months ago (1 children)

Random question - my current setup is all done through configuration.nix. Do you think it would be wise at this stage to pivot to learning about flakes and using them instead (as the other comments have suggested, flakes appear to be the way to go), prior to introducing more complexity on my system (installing chameleon, etc)?

[–] onlinepersona@programming.dev 1 points 6 months ago (1 children)

I'm sorry, but I don't use flakes, therefore my opinion on the topic should be taken with a grain of salt. To me they are unstable, the documentation worse than standard nix (which is bad enough IMO), and there a few pitfalls I've seen here and there that dissuade me from using it. However, there are people who swear by it 🤷

What I would recommend is to just try it out and see for yourself, then make the call. Maybe not straight away with your entire system, but you could try out some flakes project or something.
If you do want to start out with your system, then make a backup of /etc/nixos/, and write down somewhere which generation you're currently on, so that you can always reboot and jump into a backup configuration.

Whatever you learn with standard nix won't be lost when/if you decide to use flakes.

Anti Commercial-AI license

[–] apotheotic@beehaw.org 1 points 6 months ago (1 children)

Awesome advice, thanks again :)

[–] onlinepersona@programming.dev 2 points 6 months ago

Good luck :)

[–] verstra@programming.dev 7 points 6 months ago (1 children)

You write a nix derivation that decares it needs python and then runs the makefile. Then you can add the derivation to your configuration.nix (or temporary request it in a nix-shell).

[–] apotheotic@beehaw.org 2 points 6 months ago

Thanks, with this comment and the other comment about nix derivations I'm sure I can find some footing.

[–] Grand@programming.dev 3 points 6 months ago (1 children)

I'm also fairly new to NixOS and also nix flakes, but there isn't much to be daunted by. A nix flakes config and a normal nixos config are identical except for the inclusion of a flake.nix file which declares what inputs (sources) you're using, otherwise it's identical. I'd recommend taking a look at this video which covers NixOS configuration as well as switching to nix flakes. The timestamp I included drops you off at the nix flake section of the video.

Once you've got that understood you can either find or write a nix flake for the program you want and include it in your config and use the modules from it. If you write it you'll need to make a derivation as verstra has said in another comment, which just says how to get the source files, what dependencies are needed for building, what dependencies are needed for installing and how to build and install it. This video goes into some details about derivations which may help.

It's a lot more involved than other distros but it gives you more control. I'm not fully switched over yet but i do want to switch over as soon as i can.

[–] apotheotic@beehaw.org 3 points 6 months ago (1 children)

That's very helpful, I'll take a look. Thank you!

I'm enjoying the aspect of nixos insofar as it "forcing" me to understand what's actually going on a bit more than other distros. I decided to basically take off the training wheels and just installed a bare nixos install - no DM just a shell (which was a first for me too). I'm getting situated with hyprland (which, I'm led to understand is also a steep learning curve). Its a lot of fun but I'm doing a lot of head scratching.

[–] Grand@programming.dev 1 points 6 months ago

Glad I could help a bit

[–] kevincox@lemmy.ml 1 points 6 months ago (1 children)

I would probably just ignore the Makefile provided by upstream. It just copies files and installs dependencies.

I would take a look at existing python derivations in nixpkgs (ex a random one I picked) and try to modify it to work with your package. Once you have it working well you could even submit it to nixpkgs!

[–] apotheotic@beehaw.org 1 points 6 months ago

That's a good shout!

Although there was already a submission for this particular repo to nixpkgs, but I presume it has still not reached stable.