Skip to content

Instantly share code, notes, and snippets.

@illustris
Created June 5, 2021 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save illustris/3bd92065759f7432f95e2acfd8dc9ed6 to your computer and use it in GitHub Desktop.
Save illustris/3bd92065759f7432f95e2acfd8dc9ed6 to your computer and use it in GitHub Desktop.
An easy way to test nixpkgs changes locally using nixos-container
[illustris@illustris-thinkpad:~/src/nixpkgs]$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

[illustris@illustris-thinkpad:~/src/nixpkgs]$ git remote -v
origin  git@github.com:illustris/nixpkgs.git (fetch)
origin  git@github.com:illustris/nixpkgs.git (push)
upstream        git@github.com:NixOS/nixpkgs.git (fetch)
upstream        git@github.com:NixOS/nixpkgs.git (push)

Create a container with --nixos-path set to the nixos folder inside the local nixpkgs repo

[illustris@illustris-thinkpad:~/src/nixpkgs]$ sudo nixos-container create nixpkgstest --nixos-path ./nixos
host IP is 10.233.2.1, container IP is 10.233.2.2
these derivations will be built:
  /nix/store/bnhlb2a3dz62chlbpkaxfspxwg5cda1c-string-hosts.drv
  /nix/store/p1841b5y4z6iiqc8k26la5aflva8x2nj-hosts.drv
  /nix/store/78ywq1qadakaish64mqsdii2rzhhpd5l-unit-nscd.service.drv
  /nix/store/0q0iawc0wj7rq9yfxxqfy46bfd5li3s6-system-units.drv
  /nix/store/8s4k5brspf3svd4m3fdz9z9wk4c4wgva-etc-hostname.drv
  /nix/store/ygxnjmxw0qri2ll1vkpz64070pl7xvkk-etc.drv
  /nix/store/0n7jclfnpczdz4hj8vafafshfyqbyhqx-nixos-system-nixpkgstest-21.11pre-git.drv
building '/nix/store/8s4k5brspf3svd4m3fdz9z9wk4c4wgva-etc-hostname.drv'...
building '/nix/store/bnhlb2a3dz62chlbpkaxfspxwg5cda1c-string-hosts.drv'...
building '/nix/store/p1841b5y4z6iiqc8k26la5aflva8x2nj-hosts.drv'...
building '/nix/store/78ywq1qadakaish64mqsdii2rzhhpd5l-unit-nscd.service.drv'...
building '/nix/store/0q0iawc0wj7rq9yfxxqfy46bfd5li3s6-system-units.drv'...
building '/nix/store/ygxnjmxw0qri2ll1vkpz64070pl7xvkk-etc.drv'...
building '/nix/store/0n7jclfnpczdz4hj8vafafshfyqbyhqx-nixos-system-nixpkgstest-21.11pre-git.drv'...

Create a barebones container config

[illustris@illustris-thinkpad:~/src/nixpkgs]$ cat container.nix
{ config, pkgs, ... }:

{
  imports = [ ];
  boot.isContainer = true;
  environment.systemPackages = with pkgs; [];
}

Make changes to nixpkgs

[illustris@illustris-thinkpad:~/src/nixpkgs]$ git diff
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 6256417cb45..901179badd2 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -24179,6 +24179,7 @@ in

   heimer = libsForQt5.callPackage ../applications/misc/heimer { };

+  hell = callPackage ../applications/misc/hello { };
   hello = callPackage ../applications/misc/hello { };

   hello-wayland = callPackage ../applications/graphics/hello-wayland { };

Update container config

{ config, pkgs, ... }:

{
  imports = [ ];
  boot.isContainer = true;
  environment.systemPackages = with pkgs; [
    hell
  ];
}

Deploy new config to the container

[illustris@illustris-thinkpad:~/src/nixpkgs]$ sudo nixos-container update nixpkgstest --nixos-path ./nixos --config-file ./container.nix
these derivations will be built:
  /nix/store/1xfm4kd6sgz24c595kivh38145c7qr0h-system-units.drv
  /nix/store/k0r0hv7c2p9a8sgcmcszmvqhb5vcr25i-etc.drv
  /nix/store/zb2i9zkd11zliqg76b75vzrh8ljrzbnz-nixos-system-nixpkgstest-21.11pre-git.drv
building '/nix/store/1xfm4kd6sgz24c595kivh38145c7qr0h-system-units.drv'...
building '/nix/store/k0r0hv7c2p9a8sgcmcszmvqhb5vcr25i-etc.drv'...
building '/nix/store/zb2i9zkd11zliqg76b75vzrh8ljrzbnz-nixos-system-nixpkgstest-21.11pre-git.drv'...

Start the container, open a root shell and test the changes

[illustris@illustris-thinkpad:~/src/nixpkgs]$ sudo nixos-container start nixpkgstest

[illustris@illustris-thinkpad:~/src/nixpkgs]$ sudo nixos-container root-login nixpkgstest

[root@nixpkgstest:~]# hello
Hello, world!

[root@nixpkgstest:~]#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment