Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ into this Nix shell on changing to this directory.
In order to build both packages at the same time, run
`nix build .#{nixos,nixosLegacy}`.

Legacy-style `nix-build` and `nix-shell` can also be used; this uses
`flake-compat` under the hood.

### Tests

A collection of NixOS integration tests, created using
`pkgs.testers.runNixOSTest` is available in [nix/tests](./nix/tests). They are
also exposed as flake check attributes.

To run a particular test using flakes, run
`nix build .#checks.<arch>.<test-name>`.

If not using the flake, invoke `nix-build` directly using
`nix-build -A <test-name>` to run a test.

Large features with a significant feature scope should likely have corresponding
NixOS tests for them, so create more as needed. When creating a test, use a
suffix `.test.nix` and place it in `./nix/tests`, and it will be picked up as a
check automatically based on the filename.

Since these tests are quite expensive to run, passing them is not an automatic
CI requirement, but care should be taken to not fail the tests in this
directory, and pull requests will be rejected if they are not fixed.

### Documentation

Documentation is split into two parts:
Expand Down Expand Up @@ -84,7 +108,7 @@ suffix), and the very next commit will re-introduce the suffix.
Once a tag is created and pushed, create a GitHub release off this tag.

The version number is managed inside the Nix derivation at
[package.nix](./package.nix).
[package.nix](./nix/package.nix).

### CI

Expand Down
4 changes: 2 additions & 2 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{pkgs ? import <nixpkgs> {}}: let
flakeSelf = import ./flake-compat.nix;
flakeSelf = (import ./nix/flake-compat.nix).outputs;
inherit (pkgs.stdenv.hostPlatform) system;
in {
inherit (flakeSelf.packages.${system}) nixos nixosLegacy;

# Do not use lib.importApply here for better error tracking, since
# it causes an infinite recursion for a currently unknown reason.
module = import ./module.nix {
module = import ./nix/module.nix {
self = flakeSelf;
# If someone is using default.nix for imports, it's likely that
# they will also be using the legacy package on their system.
Expand Down
18 changes: 0 additions & 18 deletions flake-compat.nix

This file was deleted.

34 changes: 34 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 51 additions & 38 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,64 @@
flake = false;
};

flake-parts.url = "github:hercules-ci/flake-parts";

nix-options-doc.url = "github:Thunderbottom/nix-options-doc/v0.2.0";
};

outputs = {
self,
nixpkgs,
flake-parts,
...
} @ inputs: let
inherit (nixpkgs) lib;
eachSystem = lib.genAttrs lib.systems.flakeExposed;
pkgsFor = system: nixpkgs.legacyPackages.${system};
in {
packages = eachSystem (system: let
pkgs = pkgsFor system;
inherit (pkgs) callPackage;
in {
default = self.packages.${system}.nixos;

nixos = callPackage ./package.nix {
revision = self.rev or self.dirtyRev or "unknown";
};
nixosLegacy = self.packages.${system}.nixos.override {flake = false;};
});

devShells = eachSystem (system: let
pkgs = pkgsFor system;
inherit (pkgs) go golangci-lint mkShell mdbook scdoc;
inherit (pkgs.nodePackages) prettier;

nix-options-doc = inputs.nix-options-doc.packages.${system}.default;
in {
default = mkShell {
name = "nixos-shell";
nativeBuildInputs = [
go
golangci-lint

mdbook
prettier
scdoc
nix-options-doc
];
lib = inputs.nixpkgs.lib;
in
flake-parts.lib.mkFlake {inherit inputs;} {
imports = [
./nix/tests/flake-module.nix
];

systems = lib.systems.flakeExposed;

perSystem = {
self',
pkgs,
system,
...
}: {
packages = {
default = self'.packages.nixos;

nixos = pkgs.callPackage ./nix/package.nix {
revision = self.rev or self.dirtyRev or "unknown";
};

nixosLegacy = self'.packages.nixos.override {flake = false;};
};

devShells = let
inherit (pkgs) go golangci-lint mkShell mdbook scdoc;
inherit (pkgs.nodePackages) prettier;

nix-options-doc = inputs.nix-options-doc.packages.${system}.default;
in {
default = mkShell {
name = "nixos-shell";
nativeBuildInputs = [
go
golangci-lint

mdbook
prettier
scdoc
nix-options-doc
];
};
};
};
});

nixosModules.nixos-cli = lib.modules.importApply ./module.nix {inherit self;};
};
flake = {
nixosModules.nixos-cli = lib.modules.importApply ./nix/module.nix {inherit self;};
};
};
}
11 changes: 11 additions & 0 deletions nix/flake-compat.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import
(
let
lock = builtins.fromJSON (builtins.readFile ../flake.lock);
in
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)
{src = ../.;}
File renamed without changes.
15 changes: 13 additions & 2 deletions package.nix → nix/package.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
lib,
buildGoModule,
nix-gitignore,
installShellFiles,
stdenv,
scdoc,
Expand All @@ -11,7 +10,19 @@
buildGoModule (finalAttrs: {
pname = "nixos-cli";
version = "0.15.0-dev";
src = nix-gitignore.gitignoreSource [] ./.;

src = lib.fileset.toSource {
root = ../.;
fileset = lib.fileset.unions [
../go.mod
../go.sum
../Makefile
../main.go
../cmd
../doc
../internal
];
};

vendorHash = "sha256-I04cLEXLTRbxxCBemj4J6JzrtDaUQS0TuZZ8Fa0EZBk=";

Expand Down
32 changes: 32 additions & 0 deletions nix/tests/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
pkgs ? import <nixpkgs> {},
self ? (import ../flake-compat.nix).outputs,
}: let
inherit (pkgs) lib;

mkTest = test: import test {inherit pkgs self;};

findTests = dir: let
entries =
lib.genAttrs' (lib.filesystem.listFilesRecursive dir)
(name: lib.nameValuePair (builtins.toString name) (builtins.toString name));

filenames =
lib.mapAttrs'
(n: v: lib.nameValuePair (lib.strings.removePrefix (toString dir + "/") n) v)
entries;

testFiles = let
filtered' = lib.filterAttrs (n: v: lib.strings.hasSuffix ".test.nix" n) filenames;

normalizeTestName = name:
lib.strings.replaceString "/" "-"
(lib.strings.removeSuffix ".test.nix" name);
in
lib.mapAttrs' (n: v: lib.nameValuePair (normalizeTestName n) v) filtered';
in
testFiles;

tests = findTests ./.;
in
lib.mapAttrs (_: test: mkTest test) tests
15 changes: 15 additions & 0 deletions nix/tests/example.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
pkgs,
self,
}:
pkgs.testers.runNixOSTest {
name = "example-test";
nodes.machine1 = _: {
imports = [self.nixosModules.nixos-cli];
services.nixos-cli.enable = true;
};

testScript = ''
machine1.succeed("nixos features")
'';
}
16 changes: 16 additions & 0 deletions nix/tests/example.test.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
pkgs,
self,
...
}:
pkgs.testers.runNixOSTest {
name = "example-test";
nodes.machine1 = _: {
imports = [self.nixosModules.nixos-cli];
services.nixos-cli.enable = true;
};

testScript = ''
machine1.succeed("nixos features")
'';
}
5 changes: 5 additions & 0 deletions nix/tests/flake-module.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{self, ...}: {
perSystem = {pkgs, ...}: {
checks = import ./. {inherit pkgs self;};
};
}
15 changes: 1 addition & 14 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -1,14 +1 @@
(
import
(
let
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
in
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)
{src = ./.;}
)
.shellNix
(import ./nix/flake-compat.nix).shellNix
Loading