# nixosTest with flake configurations

**URL:** https://discourse.nixos.org/t/nixostest-with-flake-configurations/11542
**Category:** Uncategorized
**Created:** [February 15, 2021, 8:28pm UTC](https://discourse.nixos.org/t/nixostest-with-flake-configurations/11542 "2021-02-15T20:28:20Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![nrdxp](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nrdxp/32/8501_2.png) [@nrdxp](https://discourse.nixos.org/u/nrdxp)
#### Post date: [February 15, 2021, 8:28pm UTC](https://discourse.nixos.org/t/nixostest-with-flake-configurations/11542/1 "2021-02-15T20:28:20Z")

</div>

I am trying to write some integration tests for my machines in a flake.nix, however I seem to have run into an issue. The _nixosTest_ function and underlying _make-test-python.nix_ seem to expect a nixos module, but the flakes nixosConfigurations output wants a system that has already been evaluated by _eval-config.nix_.

I’ve tried:

```nix
nixosTest { machine = self.nixosConfigurations.NixOS; }
nixosTest { machine = self.nixosConfigurations.NixOS.config; }
nixosTest { machine = self.nixosConfigurations.NixOS.config.system.build.toplevel; }
nixosTest { machine = self.nixosConfigurations.NixOS.config.system.build.vm; }

```

to no avail.

---

<div class="post-metadata">

### Author: ![Mic92](https://discourse.nixos.org/user_avatar/discourse.nixos.org/mic92/32/5265_2.png) [@Mic92](https://discourse.nixos.org/u/Mic92)
#### Post date: [February 16, 2021, 7:29am UTC](https://discourse.nixos.org/t/nixostest-with-flake-configurations/11542/3 "2021-02-16T07:29:38Z")

</div>

I don’t think you can do it at this level. However you can have your nixos modules list in variable and pass it to both `nixosSystem { modules = myModules; }` and `nixosTest { nodes.machine = {...}: { imports = myModules;};}`

---

<div class="post-metadata">

### Author: ![nrdxp](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nrdxp/32/8501_2.png) [@nrdxp](https://discourse.nixos.org/u/nrdxp)
#### Post date: [February 20, 2021, 5:59am UTC](https://discourse.nixos.org/t/nixostest-with-flake-configurations/11542/4 "2021-02-20T05:59:39Z")

</div>

Indeed, I already knew I had to generalize [hosts](https://github.com/divnix/devos/blob/92a69cbdcbebfe5acdcab5550e99f70ba1903a2f/hosts/default.nix). Dumb question but I’m glad it has an answer for anyone as stupid as me 👍

---

<div class="post-metadata">

### Author: ![tobiasBora](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/t/9de0a6/32.png) [@tobiasBora](https://discourse.nixos.org/u/tobiasBora)
#### Post date: [October 12, 2022, 7:09am UTC](https://discourse.nixos.org/t/nixostest-with-flake-configurations/11542/5 "2022-10-12T07:09:13Z")

</div>

If anyone is interested I created an issue here [Use different versions of nixpkgs for the nodes than the one used for the nixos test · Issue #195600 · NixOS/nixpkgs · GitHub](https://github.com/NixOS/nixpkgs/issues/195600).

---

<div class="post-metadata">

### Author: ![s1341](https://discourse.nixos.org/user_avatar/discourse.nixos.org/s1341/32/4004_2.png) [@s1341](https://discourse.nixos.org/u/s1341)
#### Post date: [September 4, 2024, 4:17am UTC](https://discourse.nixos.org/t/nixostest-with-flake-configurations/11542/6 "2024-09-04T04:17:47Z")

</div>

I tried following your advice, but I run into an infinite recursion which I don’t fully understand, which I believe is related to access to `config._module.args`.

Any suggestions for how to test a `nixosConfiguration` with the testing framework?

---

<div class="post-metadata">

### Author: ![jeremiahs](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/j/d78d45/32.png) [@jeremiahs](https://discourse.nixos.org/u/jeremiahs)
#### Post date: [September 11, 2024, 1:06am UTC](https://discourse.nixos.org/t/nixostest-with-flake-configurations/11542/7 "2024-09-11T01:06:25Z")

</div>

Ive been down this rabbit hole myself and recently found a solution that fits my needs similar to you. Below is a link to a function that builds a nixosTest. I hope it helps and let me know if you are confused as to what is going on below.

**function:**

> <https://github.com/JeremiahSecrist/regula-nix/blob/c9c7e05c07a37a8c06fa024ea1631b6529e33f14/modules/regula/lib.nix#L130>

**toplevels required:**

> <https://github.com/JeremiahSecrist/regula-nix/blob/c9c7e05c07a37a8c06fa024ea1631b6529e33f14/modules/regula/default.nix#L6>

**Usage:**

> <https://github.com/JeremiahSecrist/regula-nix/blob/c9c7e05c07a37a8c06fa024ea1631b6529e33f14/modules/regula/default.nix#L51>

---

<div class="post-metadata">

### Author: ![juuso](https://discourse.nixos.org/user_avatar/discourse.nixos.org/juuso/32/7852_2.png) [@juuso](https://discourse.nixos.org/u/juuso)
#### Post date: [March 25, 2025, 10:18am UTC](https://discourse.nixos.org/t/nixostest-with-flake-configurations/11542/8 "2025-03-25T10:18:00Z")

</div>

This thread shows up on Google, so responding for future reference: the `nixosTest` function is being deprecated as of 2024: [testers.nixosTest: Deprecate by roberth · Pull Request #293891 · NixOS/nixpkgs · GitHub](https://github.com/NixOS/nixpkgs/pull/293891)

Instead, `runNixOSTest` should be used, which coincidentally makes working with nixosConfigurations easier. This tester specifically helps you with the pkgs argument, which it tries to derive automatically. And, if you have overlay applied in your flake, you can use `node.pkgsReadOnly = false;` to pass the overlayed pkgs definition into your test.

A full example here with flake.parts: [flake: add nixos tests by jhvst · Pull Request #92 · ponkila/homestaking-infra · GitHub](https://github.com/ponkila/homestaking-infra/pull/92/files)  
Moreover, in the diffset above, it’s possible to test a nixosConfiguration by doing a let binding for a `nixosSystem` before opening the flake schema. Take a look at the rest of the PR flake.nix for an example `ponkila-ephemeral-sigma`. You can test the full host by defining the `imports = ponkila-ephemeral-sigma.modules;` inside of the testable machine.

---

<div class="post-metadata">

### Author: ![weiss](https://discourse.nixos.org/user_avatar/discourse.nixos.org/weiss/32/31760_2.png) [@weiss](https://discourse.nixos.org/u/weiss)
#### Post date: [August 8, 2025, 3:27pm UTC](https://discourse.nixos.org/t/nixostest-with-flake-configurations/11542/9 "2025-08-08T15:27:00Z")

</div>

Anecdotally, I’ve had luck creating test machines that mirror flake nixosConfigurations (without a let-binding / post-hoc) by simply setting:  
`nodes.machine.imports = self.nixosConfigurations.${NAME}._module.args.modules;`

Using flake-parts, in a flake module, e.g.

```nix
{
  inputs,
  self,
  withSystem,
  ...
}:
{
  perSystem = {pkgs, system, lib, ...}: {
    checks.machine-test = pkgs.testers.runNixOSTest {
      name = "machine-test";
      nodes.machine.imports = self.nixosConfigurations.machine._module.args.modules;
      hostPkgs = lib.mkForce pkgs;
      node = withSystem "x86_64-linux" ({pkgs, inputs', config, ...}: {
        pkgs = lib.mkForce pkgs;
        specialArgs = lib.mkForce {
          inherit inputs inputs' self;
          inherit (config) packages;
        };
      });
      testScript = ''
        ...
      '';
    };
  };
}

```

Are there any downsides to this approach that y’all can see?

One might be able to do the same for `node.{specialArgs,pkgs}` (inheriting them from `nixosConfigurations.*._module`); yet to try.
