# Way to fail rebuild when home-manager activation fails?

**URL:** https://discourse.nixos.org/t/way-to-fail-rebuild-when-home-manager-activation-fails/79984
**Category:** Help
**Created:** [September 8, 2026, 3:15pm UTC](https://discourse.nixos.org/t/way-to-fail-rebuild-when-home-manager-activation-fails/79984 "2026-09-08T15:15:42Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![QuBe](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/q/ea666f/32.png) [@QuBe](https://discourse.nixos.org/u/QuBe)
#### Post date: [September 8, 2026, 3:15pm UTC](https://discourse.nixos.org/t/way-to-fail-rebuild-when-home-manager-activation-fails/79984/1 "2026-09-08T15:15:42Z")

</div>

I have home-manager set as a NixOS module, this means running `nixos-rebuild switch --sudo` additionally activates home-manager services for each user

The issue being: When home-manager activation fails, the switch still happens !  
Is there a way to make it so a failing activation also stops the switch ?

Confirmed by running `nixos-rebuild switch --sudo`, having HM fail, and then havind a new line in `nixos-rebuild list-generation`, plus this is also hinted at by:

```auto
warning: the following units failed: home-manager-qube.service

```

(since it would be an error otherwise)

---

<div class="post-metadata">

### Author: ![miangraham](https://discourse.nixos.org/user_avatar/discourse.nixos.org/miangraham/32/33148_2.png) [@miangraham](https://discourse.nixos.org/u/miangraham)
#### Post date: [September 8, 2026, 3:41pm UTC](https://discourse.nixos.org/t/way-to-fail-rebuild-when-home-manager-activation-fails/79984/2 "2026-09-08T15:41:19Z")

</div>

These might be helpful reading:

[nixos-rebuild page on the wiki](https://wiki.nixos.org/wiki/Nixos-rebuild)  
[What happens during a system switch?](https://nixos.org/manual/nixos/stable/#sec-switching-systems)

One important bit:

> If the action is `switch` or `boot`, the bootloader is updated first so the  
> configuration will be the next one to boot.

So for a single `nixos-rebuild boot/switch` command, you’ll always get the new boot entry before knowing whether HM is all green.

One approach might be to (1) run `nixos-rebuild test` which activates the new system without changing the boot stuff. You get a temporary system profile, not a new numbered generation. If everything looks good, (2) run `nixos-rebuild switch/boot` as normal. If that’s too much typing, you could make your own aliases, or a script that does step 1 then pauses and waits for you to check and hit enter before step 2, gates the switch on `systemctl --failed`, etc.

---

<div class="post-metadata">

### Author: ![QuBe](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/q/ea666f/32.png) [@QuBe](https://discourse.nixos.org/u/QuBe)
#### Post date: [September 9, 2026, 12:11pm UTC](https://discourse.nixos.org/t/way-to-fail-rebuild-when-home-manager-activation-fails/79984/3 "2026-09-09T12:11:48Z")

</div>

Oh I see, of course

I don’t mind the boot entry as much, is there a way to make the `switch` throw an error code ?  
`boot` couldn’t as you explained, but `switch` and `test` should be able to

---

<div class="post-metadata">

### Author: ![miangraham](https://discourse.nixos.org/user_avatar/discourse.nixos.org/miangraham/32/33148_2.png) [@miangraham](https://discourse.nixos.org/u/miangraham)
#### Post date: [September 9, 2026, 12:21pm UTC](https://discourse.nixos.org/t/way-to-fail-rebuild-when-home-manager-activation-fails/79984/4 "2026-09-09T12:21:25Z")

</div>

Not out of the box as far as I know. A small wrapper checking `systemctl --failed` (or `systemctl status` on specific units you care about) after the switch is the most direct way to get there that I’m aware of.

Edit: Wait, there’s one thing. You can customize [activationScripts](https://mynixos.com/nixpkgs/option/system.activationScripts) if you need to. Same basic method as the wrapper above, except a nonzero exit code from the activation script itself I believe will get a nonzero out of `nixos-rebuild`. Those are also run on boot, so don’t go too crazy with them.

---

<div class="post-metadata">

### Author: ![QuBe](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/q/ea666f/32.png) [@QuBe](https://discourse.nixos.org/u/QuBe)
#### Post date: [September 9, 2026, 12:36pm UTC](https://discourse.nixos.org/t/way-to-fail-rebuild-when-home-manager-activation-fails/79984/5 "2026-09-09T12:36:45Z")

</div>

Oh nice, I think I’ll try that !

I’ll mark solved for now, and change in case it doesn’t work
