Stop using nix-env

A small PSA about nix-env and its issues as well as what alternatives to use.

https://stop-using-nix-env.privatevoid.net/

15 Likes

I’d like to hear what a good solution for non-nixos users is too. Is nix profile the way to go for those users?

1 Like

I fully agree with this and proposed to get rid of it years ago.

2 Likes

See also Depreciate the use of nix-env to install packages? - #23 by yuu where people are describing why it can actually be useful in a few cases.

2 Likes

sometimes installing declaratively won’t succeed in hitting cache for mysterious reasons. for example, libreoffice never hits cache when it put in configuration.nix (and I’m not overriding any package in my config). so i use nix-env, now actually nix profile install, to installs it hitting cache successfully.

2 Likes

Sounds like you have your channels (and/or flake registry) jumbled up. What is cached depends on what hydra has built, but as long as you’re using the same channel/flake input there can be no difference in cache access. That, or nix would be so fundamentally broken that you can’t even build your system, but you can and so it’s likely the former.

Your user channels differ from the ones used by nixos-rebuild, and if you set a url for it your flake will point at a different input than your registry. This is why nix-env and nix profile behave differently; they install packages from completely different nixpkgs versions, unless you configure them and your system right.

I take it you use flakes now. Could you share your flake.nix inputs, and the output of nix registry list? It seems more than one person in this thread has the same problem, maybe we can resolve it in one fell swoop.

We should probably also better document how to not mess up system channels/flake inputs. It’s easy to do, feels like a good 80% of issues stem from people not realizing this. I’d also love it if the registry was set to use your system’s installed nixpkgs input by default, to make having a broken config take actual effort in the future…

It’d be sad if we kept carrying unintuitive behavior for the express purpose of some users hacking around their broken configuration. nix-env’s existence is obviously still necessary for a variety of reasons, but we should not be recommending it in general.

@TLATER

{
  description = "Yuunix: Yuu's NixOS system";

  # man nix.conf
  nixConfig = {
    extra-substituters = [
      "https://nickel.cachix.org"
      "https://nix-community.cachix.org"
      "https://nixpkgs-wayland.cachix.org"
      "https://emacsng.cachix.org"
      "https://helix.cachix.org"
    ];
    extra-trusted-public-keys = [
      "nickel.cachix.org-1:ABoCOGpTJbAum7U6c+04VbjvLxG9f0gJP5kYihRRdQs="
      "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
      "nixpkgs-wayland.cachix.org-1:3lwxaILxMRkVhehr5StQprHdEo4IrE8sRho9R9HOLYA="
      "emacsng.cachix.org-1:i7wOr4YpdRpWWtShI8bT6V7lOTnPeI7Ho6HaZegFWMI="
      "helix.cachix.org-1:ejp9KQpR1FBI2onstMQ34yogDm4OgU2ru6lIwPvuCVs="
    ];  
  };

  # Specifies other flakes which this flake depends on.
  inputs = {
    # NIXPKGS
    # nixpkgs-nixos-22-05 = {
    #   type = "github";
    #   owner = "nixos";
    #   repo = "nixpkgs";
    #   ref = "nixos-22.05";
    # };
    # nixpkgs-nixos-unstable = {
    #   type = "github";
    #   owner = "nixos";
    #   repo = "nixpkgs";
    #   # rev = "e494a908e8895b9cba18e21d5fc83362f64b3f6a";
    #   rev = "5a0e0d73b944157328d54c4ded1cf2f0146a86a5";
    # };
    # This doesn't actually set nixpkgs to follow in the flake.lock.
    # nixpkgs = nixpkgs-nixos-unstable;
    nixpkgs = {
      type = "github";
      owner = "nixos";
      repo = "nixpkgs";
      rev = "5a0e0d73b944157328d54c4ded1cf2f0146a86a5";
      # rev = "e494a908e8895b9cba18e21d5fc83362f64b3f6a";
    };
    
    nixpkgs-master.url = "github:nixos/nixpkgs/master";

    # NIXPKGS+
    nur.url = "github:nix-community/nur";
    nixpkgs-wayland.url = "github:nix-community/nixpkgs-wayland";

    # NIX LIBRARIES/TOOLS
    nickel.url = "github:tweag/nickel";
    flake-utils.url = "github:numtide/flake-utils";
    devshell.url = "github:numtide/devshell";

    # NIXPKGS TOOLS
    nixpkgs-hammering.url = "github:jtojnar/nixpkgs-hammering";

    # NIX SYSTEM
    home-manager = {
      type = "github";
      owner = "nix-community";
      repo = "home-manager";
    };

    # EMACS
    # For changing the commit hash, see:
    # https://github.com/nix-community/emacs-overlay/issues/122#issuecomment-1016778377
    # https://github.com/nix-community/emacs-overlay/issues/122#issuecomment-1193081483
    # https://hydra.nix-community.org/job/emacs-overlay/unstable/emacsNativeComp
    # NOTE: emacs-overlay and nixpkgs revisions
    # need to be the same as those used by Hydra
    # as to hit nix-community substituter/cache.
    # emacs-overlay-unstable-native-comp = {
    #   type = "github";
    #   owner = "nix-community";
    #   repo = "emacs-overlay";
    #   # https://hydra.nix-community.org/build/16426423#tabs-buildinputs
    #   rev = "12c96109a215d8f676a573c2ccbe93fb26d5b5db";
    #   inputs.nixpkgs = {
    #     type = "github";
    #     owner = "nixos";
    #     repo = "nixpkgs";
    #     rev = "614a842b74b7a1497e8cfca7c61bec38f51911b3";
    #   };
    # };

    # https://hydra.nix-community.org/job/emacs-overlay/unstable/emacsGitNativeComp
    # emacs-overlay-unstable-git-native-comp = {
    emacs-overlay = {
      type = "github";
      owner = "nix-community";
      repo = "emacs-overlay";
      # https://hydra.nix-community.org/build/16647627#tabs-buildinputs
      rev = "a12c123fd91e3206e136f5ed6f91b5390845a107";
      # no flake.lock, but doesn't work anyway for hitting the cache.
      inputs.nixpkgs = {
        type = "github";
        owner = "nixos";
        repo = "nixpkgs";
        rev = "5a0e0d73b944157328d54c4ded1cf2f0146a86a5";
      };
    };

    # emacs-overlay = emacs-overlay-unstable-git-native-comp;

    # emacs-ng = {
    #   type = "github";
    #   owner = "emacs-ng";
    #   repo = "emacs-ng";
    # };
    
    helix = {
      type = "github";
      owner = "helix-editor";
      repo = "helix";
      inputs.nixpgs.follows = "nixpkgs";
    };

    xmonad = {
      type = "github";
      owner = "xmonad";
      repo = "xmonad";
      rev = "a13a1dcee832f646aa8c6b2226f82f35240d4a70";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    
    xmonad-contrib = {
      type = "github";
      owner = "xmonad";
      repo = "xmonad-contrib";
      rev = "ee97eec17d7ce55f85003a46d34f25c9d2a1e93f";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    sops-nix.url = "github:Mic92/sops-nix";

    # 2NIX
    clj-nix.url = "github:jlesquembre/clj-nix";
    mvn2nix.url = "github:fzakaria/mvn2nix";
    poetry2nix = {
      type = "github";
      owner = "nix-community";
      repo = "poetry2nix";
      # no flake.lock
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  # Function that produces an attribute set.
  # Its function arguments are the flakes specified in inputs.
  # The self argument denotes this flake.
  outputs = (inputs@{
    self
    , nixpkgs
    , nixpkgs-master
    , nickel
    , nur
    , home-manager
    , nixpkgs-wayland
    , emacs-overlay
    , helix
    , xmonad
    , xmonad-contrib
    , sops-nix
    , clj-nix
    , mvn2nix
    , poetry2nix
    , nixpkgs-hammering
    , ...
  }:
    let
      pkgs = import nixpkgs.pkgs;
      # pkgs = nixpkgs.legacyPackages.${system};
      overlay = import overlay/default.nix;
    in {
      # Declare local packages to be available via self.my-pkgs
      my-pkgs.x86_64-linux = {
        activate-linux = pkgs.callPackage ./package/activate-linux.nix { };
        mdbook-epub = pkgs.callPackage ./package/mdbook-epub { };
      };

      # Attribute set that nixos-rebuild looks for.
      nixosConfigurations = {
        yy = nixpkgs.lib.nixosSystem {
          system = "x86_64-linux";

          specialArgs = {
            inherit
              inputs;
          };

          modules = [
            {
              nixpkgs.overlays = [
                overlay
              ];
            }

            (import ./host/yy/configuration.nix)

            # Adds a nur configuration option.
            # Use `config.nur` for packages like this:
            # ({ config, ... }: {
            #   environment.systemPackages = [
            #     config.nur.repos.mic92.hello-nur
            #   ];
            # })
            nur.nixosModules.nur

            home-manager.nixosModules.home-manager
            {
              home-manager = {
                extraSpecialArgs = {
                  inherit
                    inputs;
                };
                useUserPackages = true;
                useGlobalPkgs = true;
                # home-manager.users.username.imports = [ ./home.nix ];
              };
            }

            sops-nix.nixosModules.sops
          ];
        };
      };
    }
  );
}
nix registry list
system flake:nixpkgs path:/nix/store/ksa0wycynd4jzxa5nf4g37rnmpvw2akp-source
system flake:self path:/nix/store/4jskv6bh4anwfav9isxccvm8pw1yhzxm-source
global flake:agda github:agda/agda
global flake:arion github:hercules-ci/arion
global flake:blender-bin github:edolstra/nix-warez?dir=blender
global flake:dreampkgs github:nix-community/dreampkgs
global flake:dwarffs github:edolstra/dwarffs
global flake:emacs-overlay github:nix-community/emacs-overlay
global flake:fenix github:nix-community/fenix
global flake:flake-parts github:hercules-ci/flake-parts
global flake:flake-utils github:numtide/flake-utils
global flake:gemini github:nix-community/flake-gemini
global flake:hercules-ci-effects github:hercules-ci/hercules-ci-effects
global flake:hercules-ci-agent github:hercules-ci/hercules-ci-agent
global flake:home-manager github:nix-community/home-manager
global flake:hydra github:NixOS/hydra
global flake:mach-nix github:DavHau/mach-nix
global flake:nimble github:nix-community/flake-nimble
global flake:nix github:NixOS/nix
global flake:nix-darwin github:LnL7/nix-darwin
global flake:nixops github:NixOS/nixops
global flake:nixos-hardware github:NixOS/nixos-hardware
global flake:nixos-homepage github:NixOS/nixos-homepage
global flake:nixos-search github:NixOS/nixos-search
global flake:nur github:nix-community/NUR
global flake:nixpkgs github:NixOS/nixpkgs/nixpkgs-unstable
global flake:templates github:NixOS/templates
global flake:patchelf github:NixOS/patchelf
global flake:poetry2nix github:nix-community/poetry2nix
global flake:nix-serve github:edolstra/nix-serve
global flake:nickel github:tweag/nickel
global flake:bundlers github:NixOS/bundlers
global flake:pridefetch github:SpyHoodle/pridefetch
global flake:helix github:helix-editor/helix

i thought maybe it could be a nixos option or something that could be messing up this. another person who had similar issue found out it was because of overriding a package by setting a nixos option. i really have no idea what option it could be if it is really caused by setting an option in my case.

I had a similar problem: in my case I was overriding a seemingly unrelated package and this caused a rebuild of a single application on every channel update
[I found the root cause] just by (educated) guessing. I’m not aware of a nice tool to investigate this sort of issues. In my case it was xserver.extraLayouts overriding xkeyboard_config and zathura being rebuilt
@rnhmjoj:maxwell.ydns.eu, You're invited to talk on Matrix

i think i’ll try removing that (import ./host/yy/configuration.nix) and adding only environment.systemPackages = [pkgs.libreoffce]; to the flake modules to try eliminating one of the possibilities. cool thing we can rollback in nixos, otherwise i would not do it.

Hosted by Flying Circus.