nix-output-monitor icon indicating copy to clipboard operation
nix-output-monitor copied to clipboard

Create a nixos module

Open SuperSandro2000 opened this issue 2 years ago • 11 comments

It would be cool if there would be a nixos module which would just enable nom by creating the aliases. It could also maybe hook into nixos-rebuild.

SuperSandro2000 avatar Dec 04 '22 01:12 SuperSandro2000

Yeah, would be cool. Main problem is, that currently nom is not robust enough as a full drop in for the nix:

  • [ ] For most nix subcommands nom will just error out.
  • [ ] There are nix subcommands which report progress, for which nom has no support, yet.
  • [ ] I think it’s likely that nom shell can break in rare use cases (because it messes with the args the user gives), then having no way to use nix shell would be bad.

Probably there are more issues, and I am not sure if it’s worth it to fix all those.

One other point is, that I don’t know how to write such a module in a way that it actually replaces nix everywhere it’s called. It could only replace nix in the PATH and in known locations, like nixos-rebuild (for which I am uncertain if a PR would be accepted). That might break users expectations when they e.g. run nixpkgs-review which might use an absolute nix path.

maralorn avatar Dec 05 '22 09:12 maralorn

The module could only add a wrapper function to /etc/profile which could be turned off easily with command.

SuperSandro2000 avatar Dec 05 '22 12:12 SuperSandro2000

I have to confess, I don’t exactly understand what you mean with that.

maralorn avatar Dec 10 '22 01:12 maralorn

There are two ways how we could alias nix to nom:

  • symlink nom to nix. You could then get the old nix by using which -a nix and choose the second result.
  • create a bash function which shadows nix. With that you get the original nix with command nix.

In both cases only subcommands nom understands should be forwarded to it, everything else including when running in a none interactive shell should be handled by nix.

SuperSandro2000 avatar Dec 12 '22 09:12 SuperSandro2000

Somewhat related, I was toying with making a nom-rebuild command from the nixos-rebuild, but we need an equivalent to nix eval for this to work properly.

See https://github.com/KoviRobi/nixpkgs/commit/308f84ff16cb962de509dc9f2333cc7ddc79aeac then an overlay like

final: prev:
{
  nom-rebuild = (
    import
      (builtins.getFlake "github:KoviRobi/nixpkgs/308f84ff16cb962de509dc9f2333cc7ddc79aeac")
      { inherit (final) system; }
  ).nixos-rebuild.overrideAttrs (_: {
    name = "nom-rebuild";
    nix3_build = "${final.nix-output-monitor}/bin/nom build";
    nix_build = "${final.nix-output-monitor}/bin/nom-build";
  });
}

but this doesn't seem to work, I think it's because the build is being done by nix eval inside nixFlakeBuild maybe, haven't looked into this properly.

KoviRobi avatar Mar 09 '23 00:03 KoviRobi

I just went the easy path and patched nixos-rebuild ;)

https://github.com/SuperSandro2000/nixpkgs/commit/449114c6240520433a650079c0b5440d9ecf6156

SuperSandro2000 avatar Mar 09 '23 00:03 SuperSandro2000

Ah that works because it is still called nixos-rebuild and config.system.build.nixos-rebuild refers to it, my one reverts to the nix build because I've renamed it nom-rebuild and the re-exec runs the normal nixos-rebuild

KoviRobi avatar Mar 11 '23 04:03 KoviRobi

@SuperSandro2000: I just went the easy path and patched nixos-rebuild ;)

Is there a way to replicate this behavior without having to build a system from a nixpkgs fork with the patched nixos-rebuild?


Since this issue is marked as help wanted, is there a clear path to completion for this? From @maralorn's initial reply, it sounds like there are some dependent issues that would need to be resolved (e.g., https://github.com/maralorn/nix-output-monitor/issues/109, https://github.com/maralorn/nix-output-monitor/issues/106, others)?

ChanceHarrison avatar Oct 03 '23 21:10 ChanceHarrison

Well, the "help wanted" partially relates to the fact that I don’t know exactly how to do it.

As a rough sketch two things need to happen:

a) We need to figure out how nom should behave. b) We need to figure out how to pass nom into nixos-rebuild.

Preferably we do this without breaking anyone's possibility to rebuild their system if nom screws something up.

Yeah, #109 and #106 are probably a step in the right direction to solve a). b) should maybe happen in nixpkgs and not in nom.

There are obviously solutions for all of this, as various people have found one or another way to implement this for themselves. I just haven’t had the time to look into how to do this "the right way"(tm). I would be happy about any input in that regard.

maralorn avatar Oct 03 '23 23:10 maralorn

For b), that's why I created https://github.com/KoviRobi/nixpkgs/commit/308f84ff16cb962de509dc9f2333cc7ddc79aeac but it's too ugly to merge back I think. Seems like https://github.com/SuperSandro2000/nixpkgs/commit/449114c6240520433a650079c0b5440d9ecf6156 solves the problem by moving "${flakeFlags[@]}" after the sub-command to nix/nom. Possibly the argument parser should support the flags before the subcommand.

flakeFlags is the following

❯ sh -x nixos-rebuild switch
[...]
+ flakeFlags=(--extra-experimental-features 'nix-command flakes')
[...]
+ nix --extra-experimental-features 'nix-command flakes' build --out-link /tmp/nixos-rebuild.aSGRCq/nixos-rebuild '/home/rmk35/nixos#nixosConfigurations."pc-nixos-a".config.system.build.nixos-rebuild'

I suspect the problem is https://github.com/maralorn/nix-output-monitor/blob/main/exe/Main.hs#L69 looking for build as the first element of the args-list, instead of the first non-option argument

If we could get that patched, then we could merge a simpler version of nixos-rebuild than mine into nixpkgs, which just abstracts over the nix, nix-build and nix-shell commands (probably also needs #109)

KoviRobi avatar Oct 09 '23 22:10 KoviRobi

Excellent input, thanks. We can certainly fix that.

maralorn avatar Oct 09 '23 22:10 maralorn