NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
18.39k stars 14.34k forks source link

Distribute `nixos-rebuild` with non-nixos nix distribution #44135

Closed mkaito closed 4 years ago

mkaito commented 6 years ago

I'm currently working on a web app project that is deployed to a NixOS server. Our deployment consists of running nixos-rebuild --target-host to push things to the server.

The problem is, nixos-rebuild is only available on NixOS proper, not on the standalone nix distribution. I had to deal with some grumpy people when I explained that they needed to get themselves a NixOS VM in order to run deployments.

I don't think there's anything preventing at least the --target-host part of nix-rebuild being available as part of the standard nix distribution.

lukateras commented 6 years ago

I think that at the very least we should expose it at top-level.

mkaito commented 6 years ago

I expect these to come up as counter arguments:

Just use nixops you dimwit

I tried, but I couldn't get it to work. And nixos-rebuild is super easy to use. Just point at a configuration.nix and it even builds everything remotely, which my peers with laptops appreciate.

CI/CD pipeline

Does such a thing even exist for NixOS?

srhb commented 6 years ago

If it's interesting to you, nixos was recently exposed as a top level attribute, so you could do

nix-build -E 'let pkgs = import <nixpkgs> {}; in (pkgs.nixos {}).nixos-rebuild'
Mic92 commented 6 years ago

nix-shell version:

$ nix-shell -E 'with import <nixpkgs> {}; stdenv.mkDerivation { name = "foo"; buildInputs = [ (nixos {}).nixos-rebuild ]; }'

or:

$ nix-shell -E 'with import <nixpkgs> {}; stdenv.mkDerivation { name = "foo"; buildInputs = [ (nixos {}).nixos-rebuild ]; }' --command "nixos-rebuild dry-build"
mkaito commented 6 years ago

This does the job:

#!/usr/bin/env bash

nix-shell -E 'with import <nixpkgs> {}; stdenv.mkDerivation { name = "foo"; buildInputs = [ (nixos {}).nixos-rebuild ]; }' --run "nixos-rebuild $*"

Drop it in a file nixos-rebuild, put on path, done. Thanks @Mic92 !

Mic92 commented 6 years ago

It can be even simplified:

$ nix-shell -p '(nixos{}).nixos-rebuild'
mkaito commented 6 years ago

Nice! Can we put this in the Nix distribution?

Mic92 commented 6 years ago

I backported d9f1e37ec72f0b4d55ef8d8f24aa4fc499fe878c:

Now you can do:

$ nix-shell -p 'nixos-rebuild'

This will be part of the upcoming release 18.09.

matthewbauer commented 6 years ago

@Mic92 That was reverted by @edolstra due to evaluation concerns (every nix-env -qa has to evaluate a full nixos).

I think the best solution for this would be to move nixos-rebuild to all-packages.nix & rework NixOS to use that.

Mic92 commented 6 years ago

Makes sense. I was not aware of that. I also backported the revert: 51b62e6a8c9

However:

nix-shell -p '(nixos{}).nixos-rebuild'

should be easy enough.

adrian-gierakowski commented 4 years ago

@mkaito @Mic92 is this still working for you?

when I run:

env NIXOS_CONFIG=$(pwd)/configuration.nix nix-shell -p '(nixos{}).nixos-rebuild' --run 'nixos-rebuild switch --target-host $MY_HOST'

I get the following error:

/nix/store/17843hfbsnyqxkjx6v7sdrln855npyll-nixos-rebuild/bin/nixos-rebuild: line 268: systemctl: command not found
building Nix...
unix_listener: path "/var/folders/dm/3dxc156d67l5241z0y2tt8h80000gn/T/nixos-rebuild.FS9L0q/ssh-130.211.114.86.iLXNmuqNiJwgmhy5" too long for Unix domain socket
/nix/store/17843hfbsnyqxkjx6v7sdrln855npyll-nixos-rebuild/bin/nixos-rebuild: line 343: /var/folders/dm/3dxc156d67l5241z0y2tt8h80000gn/T/nixos-rebuild.FS9L0q/nix/bin/nix-instantiate: cannot execute binary file: Exec format error
building the system configuration...
/nix/store/17843hfbsnyqxkjx6v7sdrln855npyll-nixos-rebuild/bin/nixos-rebuild: line 181: /var/folders/dm/3dxc156d67l5241z0y2tt8h80000gn/T/nixos-rebuild.FS9L0q/nix/bin/nix-instantiate: cannot execute binary file: Exec format error

looks to me like it might be similar to: https://github.com/NixOS/hydra/pull/641

mkaito commented 4 years ago

Currently using this:

nix-shell -E "with import <nixpkgs> {}; mkShell { buildInputs = [ (nixos {}).nixos-rebuild ]; }" --run "nixos-rebuild $*"

And yes, it works.

Mic92 commented 4 years ago

@mkaito @Mic92 is this still working for you?

when I run:

env NIXOS_CONFIG=$(pwd)/configuration.nix nix-shell -p '(nixos{}).nixos-rebuild' --run 'nixos-rebuild switch --target-host $MY_HOST'

I get the following error:

/nix/store/17843hfbsnyqxkjx6v7sdrln855npyll-nixos-rebuild/bin/nixos-rebuild: line 268: systemctl: command not found
building Nix...
unix_listener: path "/var/folders/dm/3dxc156d67l5241z0y2tt8h80000gn/T/nixos-rebuild.FS9L0q/ssh-130.211.114.86.iLXNmuqNiJwgmhy5" too long for Unix domain socket
/nix/store/17843hfbsnyqxkjx6v7sdrln855npyll-nixos-rebuild/bin/nixos-rebuild: line 343: /var/folders/dm/3dxc156d67l5241z0y2tt8h80000gn/T/nixos-rebuild.FS9L0q/nix/bin/nix-instantiate: cannot execute binary file: Exec format error
building the system configuration...
/nix/store/17843hfbsnyqxkjx6v7sdrln855npyll-nixos-rebuild/bin/nixos-rebuild: line 181: /var/folders/dm/3dxc156d67l5241z0y2tt8h80000gn/T/nixos-rebuild.FS9L0q/nix/bin/nix-instantiate: cannot execute binary file: Exec format error

looks to me like it might be similar to: NixOS/hydra#641

I never tested this on non-Linux targets. This might require some other flags. Adding --no-build-nix might help.

stale[bot] commented 4 years ago

Hello, I'm a bot and I thank you in the name of the community for opening this issue.

To help our human contributors focus on the most-relevant reports, I check up on old issues to see if they're still relevant. This issue has had no activity for 180 days, and so I marked it as stale, but you can rest assured it will never be closed by a non-human.

The community would appreciate your effort in checking if the issue is still valid. If it isn't, please close it.

If the issue persists, and you'd like to remove the stale label, you simply need to leave a comment. Your comment can be as simple as "still important to me". If you'd like it to get more attention, you can ask for help by searching for maintainers and people that previously touched related code and @ mention them in a comment. You can use Git blame or GitHub's web interface on the relevant files to find them.

Lastly, you can always ask for help at our Discourse Forum or at #nixos' IRC channel.

Mic92 commented 4 years ago

I think we gave enough work-arounds on how to achieve this.

mkaito commented 4 years ago

Well, that's a shame, but such is life.

Mic92 commented 4 years ago

Sorry but apparently bringing nixos modules into all-packages scope apparently makes nix evaluation a lot slower. There are people who want to use nix on machines with little RAM. I don't think its too much to ask to use one of one-liners posted above. If you are interested in improving the situation consider making them more visible by adding them to the manual or the nixos wiki.

kevincox commented 4 years ago

It seems like bringing nixos into scope was a side effect of how it was implemented. Wouldn't it be possible to move nixos-rebuild into a regular nixpkgs package and reference that from nixos?

(~On another note does anyone know how to install it using nix-env instead of nix-shell? I can't get it to work~ nix-env -i -E 'e: ((e.nixpkgs {}).nixos{}).nixos-rebuild')

Mic92 commented 4 years ago

It seems like bringing nixos into scope was a side effect of how it was implemented. Wouldn't it be possible to move nixos-rebuild into a regular nixpkgs package and reference that from nixos?

(~On another note does anyone know how to install it using nix-env instead of nix-shell? I can't get it to work~ nix-env -i -E 'e: ((e.nixpkgs {}).nixos{}).nixos-rebuild')

Yes. nix-env also works.