Changing priority of system packages?

I have python defined in two different places for one host. One is just straight python311 and the other is python311 with an included module (podman). However, the system decided that it will use just the straight python311 (from packages.nix) without the module as the default python available. Is there any way to change which python is used as the system default when the two sets are merged? Config:

hosts/modules/common/packages.nix

{ pkgs, ... }:{
    environment.systemPackages = with pkgs; [
      bottom
      python311
    ...
    ];
}

modules/podman.py

{
  pkgs,
  ...
}: let
  podman-py = p:
    with p; [
      pkgs.python311Packages.podman
    ];
in {
  environment.systemPackages = with pkgs; [
    (python311.withPackages podman-py)
  ];
....
}

Thanks!

There are a couple of things I think could help you:

  • You can use lib.mkBefore in a module value to prepend it, instead of appending it, to the values it merges with. (The more general version of this is lib.mkOrder, which also accepts a numeric argument.) Earlier entries in environment.systemPackages should take precedence over later ones.

  • You can give your customized Python a higher priority (‘higher’ means, confusingly, a lower numeric value) like so:

    (python311.withPackages podman-py // { meta.priority = 1; })
    

    or your default Python a lower priority:

    (python311 // { meta.priority = 10; })
    

    The default priority is 5.

Also, unrelated tip: instead of

podman-py = p:
  with p; [
    pkgs.python311Packages.podman
  ];

prefer

podman-py = p:
  [
    p.podman
  ];
1 Like

Thanks so much! The meta.priority bit worked perfectly.

For a long list of additional python modules, wouldn’t be cleaner to use

podman-py = p:
  with p; [
      podman
      grip
      ipython
      ...
  ];

instead of leaving out the with and having to prepend p. to each package?

Thanks!

An unusual fact about with is that it will not shadow outer bindings; if you already have a top-level variable named podman, then the podman under with p; will refer to that and not p.podman. with does shadow other uses of with, though.

I find the resulting confusion more costly than writing p. before each Python package. YMMV. The main thrust of my tip was not to repeat the pkgs.python311Packages bit, when you have a p there for this purpose (and which also prevents you from accidentally mixing Python versions).

1 Like