Misterio77 / nix-starter-configs

Simple and documented config templates to help you get started with NixOS + home-manager + flakes. All the boilerplate you need!
Creative Commons Zero v1.0 Universal
2.48k stars 126 forks source link

Home-manager with a package from unstable #6

Closed voidzero closed 1 year ago

voidzero commented 1 year ago

Dear Misterio,

How do I install a package from unstable, in a home.nix file? Just one package, I prefer to keep the others at 22.05.

Previously I did it like this:

{ inputs, lib, config, pkgs, ... }:
let
  unstable = import <nixpkgs-unstable> { config = { allowUnfree = true; }; };
in
{
(...)

But this is considered impure in the world of flakes.

Sorry for my n00b question.

Mark

voidzero commented 1 year ago

My home-markvd.nix config (and my other config files) can be found here: https://github.com/voidzero/chopin/blob/master/dotfiles/home-manager/home-markvd.nix

voidzero commented 1 year ago

Also, for some reason I am getting the following message when running this command:

$ home-manager switch --flake '.#markvd@nixos-vmware'
warning: Git tree '/home/markvd/chopin' is dirty
trace: warning: You are using

  Home Manager version 22.11 and
  Nixpkgs version 22.05.

Using mismatched versions is likely to cause errors and unexpected
behavior. It is therefore highly recommended to use a release of Home
Manager than corresponds with your chosen release of Nixpkgs.

If you insist then you can disable this warning by adding

  home.enableNixpkgsReleaseCheck = false;

to your configuration.

But when I do home-manager --version the output is 22.05. Weird. Why is this happening?

voidzero commented 1 year ago

I bit the bullet and tried it with --impure. Now I get this:

$ home-manager switch --flake '.#markvd@nixos-vmware' --impure
trace: warning: You are using

  Home Manager version 22.11 and
  Nixpkgs version 22.05.

Using mismatched versions is likely to cause errors and unexpected
behavior. It is therefore highly recommended to use a release of Home
Manager than corresponds with your chosen release of Nixpkgs.

If you insist then you can disable this warning by adding

  home.enableNixpkgsReleaseCheck = false;

to your configuration.

error: attribute 'numbers' missing

       at /nix/store/rwlglfn25kylqr7sim7wkgqggnbcicii-source/modules/services/picom.nix:144:14:

          143|     activeOpacity = mkOption {
          144|       type = types.numbers.between 0 1;
             |              ^
          145|       default = 1.0;
(use '--show-trace' to show detailed location information)

At this point I feel like pulling out my hairs and giving up :sweat_smile:

voidzero commented 1 year ago

I asked on Reddit in this thread and got some very useful help. In particular the comment that we are close enough to NixOS and home-manager version 22.11 to use unstable made sense. So I have both the system and home-manager working properly now by using those versions - code can be seen here.

I am not yet sure what to do once 22.11 is the current version and I want to use a package from unstable after that. But at least I got the initial code to work, so that's nice.

Misterio77 commented 1 year ago

Heyy, sorry for the wait; let me get you an example on how to do it.

Misterio77 commented 1 year ago

Maybe this could be of help? https://github.com/Misterio77/nix-starter-configs/issues/4#issuecomment-1287903107

voidzero commented 1 year ago

Thanks! Ah okay, this would be for use with the standard - not minimal - flake setup. I should probably switch to the standard setup anyway because I plan to setup and manage other systems in my flake too some time in the future. Let me try this out and I'll get back on this. (Might take me a while, I am still recovering from covid and I am trying to learn NixOS and Flakes in between headaches :face_with_thermometer: )

Misterio77 commented 1 year ago

I'm setting up a dual-nixpkgs template based on standard, I'll let you know when it's done. Shouldn't take long

Hope you get better soon!

Misterio77 commented 1 year ago

Here's the template: https://github.com/Misterio77/nix-starter-configs/blob/main/multi-nixpkgs/flake.nix

In the example, I'm using unstable + master branches, but you may easily use 22.05 + unstable instead; the only thing to be aware is that home-manager.lib.homeManagerConfiguration's interface changed quite a bit from 22.05 to now.

First you add the extra input: https://github.com/Misterio77/nix-starter-configs/blob/main/multi-nixpkgs/flake.nix#L7

There's a lot of different ways to consume its packages (and modules, if you want); in this example I added the bleeding edge master packages as an overlay under the scope master, like so: https://github.com/Misterio77/nix-starter-configs/blob/main/multi-nixpkgs/overlays/default.nix#L6

You would then be able to access the master packages in your home configs like so:

{ pkgs, ... }: {
  home.packages = [
    pkgs.master.neofetch
    pkgs.pfetch
  ];
  programs.neovim.package = pkgs.master.neovim;
}

A repl example you can try out:

$ nix repl
Welcome to Nix 2.11.0. Type :? for help.

nix-repl> :lf github:misterio77/nix-starter-configs?dir=multi-nixpkgs
Added 16 variables.

nix-repl> pkgs = homeConfigurations."your-username@your-hostname".pkgs

nix-repl> pkgs.flow
«derivation /nix/store/jimkg20hpc9mdy51wgxs9b5fwnmi16vl-flow-0.191.0.drv»

nix-repl> pkgs.master.flow
«derivation /nix/store/7lx0khm8kkbix197hp70cdzd3fxp3ymd-flow-0.192.0.drv»

You can alternatively:

voidzero commented 1 year ago

Alright, a lot for me to try out, thank you. First I'll modify my config and will have this ready in a few minutes.

I notice in the multi-nixpkgs/flake.nix we are not setting config.allowUnfree anymore. Where should I add this now? edit never mind, I see it already (./nixpkgs-config.nix)

Misterio77 commented 1 year ago

I've moved it to nixpkgs-config.nix with https://github.com/Misterio77/nix-starter-configs/pull/7

Misterio77 commented 1 year ago

TLDR: Instantiating nixpkgs right on the flake and passing it to the configs is not good practice; it's better to use nixpkgs.* options.

So I created a file that has common configuration we import on both home-manager and nixos, that way we have a centralized way to apply overlays and set options (such as allowUnfree), while avoiding the issues we would have instantiating nixpkgs directly.

voidzero commented 1 year ago

At first I got the following error:

$ sudo nixos-rebuild switch --flake '.#nixos-vmware'
error: value is a function while a set was expected

       at /nix/store/zz9rbrbh8jmkdnsn83qc67ywkbbcjj12-source/nixpkgs-config.nix:8:16:

            7|     # Overlays is an attrset, convert to a list
            8|     overlays = builtins.attrValues overlays;
             |                ^
            9|   };

It makes me guess that we don't need to convert overlays to a list, so I removed that line. I also had to add system="x86_64-linux"; under the hostname under nixosConfigurations in my flake.nix (because I am using 22.05 I suppose?)

The system now builds, and I have pushed my updates to the master branch of https://github.com/voidzero/chopin. This is using multi-nixpkgs now.

home-manager does not build, probably because there are differences between 22.05 and 22.11? First it complains about no argument system in homeConfigurations. So I added it there too. Now it complains about the following:

$ home-manager switch --flake '.#markvd@nixos-vmware'                             
error: 'homeManagerConfiguration' at /nix/store/zjxmj1b43j32zkvj1821a59svfgsknbg-source/flake.nix:44:36 called without required argument 'configuration'

       at /nix/store/72qczrd6qnqmvix2ih7947kia5yah607-source/flake.nix:69:33:

           68|         # FIXME replace with your username@hostname
           69|         "markvd@nixos-vmware" = home-manager.lib.homeManagerConfiguration {
             |                                 ^
           70|           system = "x86_64-linux";

Hope you are not getting tired of me yet 😅

Misterio77 commented 1 year ago

Hey,

So I removed that line. I also had to add system="x86_64-linux"; under the hostname under nixosConfigurations in my flake.nix (because I am using 22.05 I suppose?)

Yup, that's right.

It makes me guess that we don't need to convert overlays to a list

In your case, yeah. The newer update changed overlays/default.nix to an attribute set of overlays, but your is simply a single (composed) overlay, so no need to convert it. Just change the line to: overlays = [ overlays ]. Nixpkgs expectes overlays to be a list of overlays.

Do keep in mind that the overlays output in the flake is supposed to be an attrset of overlays, so, if prefer to keep overlays/default.nix the way it is, change this line to:

 overlays.default = import ./overlays { inherit inputs; };

If you do, nixpkgs-config.nix's overlays = builtins.attrValues overlays; should work as expected.

$ home-manager switch --flake '.#markvd@nixos-vmware'
error: 'homeManagerConfiguration' at /nix/store/zjxmj1b43j32zkvj1821a59svfgsknbg-source/flake.nix:44:36 called without required argument 'configuration'

Yeah, the homeManagerConfiguration function changed quite a bit from 22.05 to 22.11/unstable, so you must match it. I can't seem to find the 22.05 release hm manual, but here's the function definition: https://github.com/nix-community/home-manager/blob/release-22.05/flake.nix#L44, should help you out.

Hope you are not getting tired of me yet sweat_smile

Don't worry! Not a problem at all. I might take a few mins to a few hours to respond, though.

voidzero commented 1 year ago

Alright, a real head scratcher. With regards to 22.05 it looks like the homeManager configuration is weirdly different for when it is in a flake or when it is in a home.nix. Because in home.nix, you can set things like home.username, home.homeDirectory, but in the flake you have to configure these separately as well.

I changed the code in flake.nix to the following, just to test:

homeConfigurations = { 
  "markvd@nixos-vmware" = home-manager.lib.homeManagerConfiguration {
    system = "x86_64-linux";
    pkgs = nixpkgs.legacyPackages.x86_64-linux;
    extraSpecialArgs = { inherit inputs; };
    configuration = ./home-manager/home-markvd.nix;
    homeDirectory = "/home/markvd";
    username = "markvd";
  };  
};

Now I run:

$ home-manager --version                                          
22.05

$ home-manager switch --flake '.#markvd@nixos-vmware'             
warning: Git tree '/home/markvd/chopin' is dirty
error: The option `home.stateVersion' has conflicting definition values:
       - In `/nix/store/crzflhwq1jgh9giga32d77dm41y2alfj-source/home-manager/home-markvd.nix': "22.05"
       - In `<unknown-file>': "20.09"
(use '--show-trace' to show detailed location information)

I have no idea where 20.09 comes from.

Misterio77 commented 1 year ago

You should set stateVersion on the flake function call as well https://github.com/nix-community/home-manager/issues/2073.

homeConfigurations = { 
  "markvd@nixos-vmware" = home-manager.lib.homeManagerConfiguration {
    system = "x86_64-linux";
    pkgs = nixpkgs.legacyPackages.x86_64-linux;
    extraSpecialArgs = { inherit inputs; };
    configuration = ./home-manager/home-markvd.nix;
    homeDirectory = "/home/markvd";
    username = "markvd";
    stateVersion = "22.05";
  };  
};
voidzero commented 1 year ago

That works and now we are on the next problem: when home-markvd.nix contains vivaldi, I get a complaint about a missing allowUnfree. Apparently there is a bug about the allowUnfree issue described here: https://github.com/nix-community/home-manager/issues/2942.

I have added allowUnfreePredicate = (_: true); in nixpkgs-config.nix, and this makes vivaldi (the stable version) work.

I also found that I can use extraModules in homeConfigurations, so I added this to use the unstable overlay there. My config now looks as follows:

homeConfigurations = {
  "markvd@nixos-vmware" = home-manager.lib.homeManagerConfiguration {
    system = "x86_64-linux";
    pkgs = nixpkgs.legacyPackages.x86_64-linux;
    extraSpecialArgs = { inherit inputs; };
    configuration = ./home-manager/home-markvd.nix;
    homeDirectory = "/home/markvd";
    username = "markvd";
    stateVersion = "22.05";
    extraModules = (builtins.attrValues homeManagerModules) ++ [
      (import ./nixpkgs-config.nix { inherit overlays; })
    ];
  };
};

Now I can change vivaldi to vivaldi-unstable, and I see that it will try to install the correct version of vivaldi, but, here it still complains about allowUnfree. So it is not reading the allowUnfreePredicate option either, meaning that I cannot install vivaldi-unstable. This is where I am stuck.

Misterio77 commented 1 year ago

I think you'll need to instantiate nixpkgs-unstable with allowUnfree true, this should do the trick: https://github.com/voidzero/chopin/pull/1

voidzero commented 1 year ago

@Misterio77 you're my hero! This works! I can now run this flake without impure, and it installs the packages from unstable. Much thanks!! :+1: :+1:

Misterio77 commented 1 year ago

You're very welcome :)

voidzero commented 1 year ago

Maybe beyond the issue but just for my education, how did you learn this solution?

Misterio77 commented 1 year ago

It's just something I came to the conclusion to, after knowing a bit about how nix and nixpkgs work. It's a little hard to enumerate which resources I used to get to know them, but I'd recommend you read nix pills; there's a lot of other good guides at the NixOS.org learn page, as well.

voidzero commented 1 year ago

Will do. Thanks again. I'll close this bug. :rose: