Mic92 / nixos-shell

Spawns lightweight nixos vms in a shell
MIT License
684 stars 39 forks source link

multihoming #9

Closed teto closed 5 years ago

teto commented 5 years ago

I was pondering between using this and keep using nixops but this seems leaner (my nixops is full of libvirt hacks) and I also like that the host store can be accessed.

I am trying to create a multihomed vm.nix by adding virtualisation.vlans = [ 2 1 3 4 ]; in my vm.nix but I still see only one eth0 in the vm.

I installed nixos-shell via nix-env and the name is not very inspiring so here is patch changing the name and removing some trailing whitespace if you want.

diff --git a/default.nix b/default.nix
index 0224ee5..3440675 100644
--- a/default.nix
+++ b/default.nix
@@ -1,6 +1,7 @@
 with import <nixpkgs> {};
 stdenv.mkDerivation {
-  name = "env";
+  pname = "nixos-shell";
+  version = "20190604";
   src = ./.;
   buildInputs = [ bash ];
   preConfigure = ''
diff --git a/share/nixos-shell/nixos-shell.nix b/share/nixos-shell/nixos-shell.nix
index 4e353f7..ae407fb 100644
--- a/share/nixos-shell/nixos-shell.nix
+++ b/share/nixos-shell/nixos-shell.nix
@@ -43,15 +43,15 @@ in {
                 type = types.path;
                 description = "Target on the guest.";
               };
-  
+
               inherit cache;
-  
+
               tag = mkOption {
                 type = types.str;
                 internal = true;
               };
             };
-  
+
             config.tag = lib.mkDefault (
               builtins.substring 0 31 ( # tags must be shorter than 32 bytes
                 "a" + # tags must not begin with a digit
Mic92 commented 5 years ago

The patch was applied to master in https://github.com/Mic92/nixos-shell/commit/ce3bbd0202008d2b26f36f989a20aeeba5d911ed and https://github.com/Mic92/nixos-shell/commit/c20dddeed9ef4db3ed9b20e9cec05a86f2040391

teto commented 5 years ago

what about the multihoming issue ? it seems to work fine with the nixos/tests infrastructure. There seems to be some related code (to vlans) in nixos/lib/testing.nix but it doesn't seem called in nixos-shell

teto commented 5 years ago

I managed to come up in my vm.nix with sthg like

let
  vlans = [ 1 2];
  zeroPad = n: if n < 10 then "0${toString n}" else toString n;

  # see https://wiki.qemu.org/Documentation/Networking 's doc
  qemuNICFlags = nic: net: machine:
    [ "-device virtio-net-pci,netdev=vlan${toString nic},mac=52:54:00:12:${zeroPad net}:${zeroPad machine}"
      "-netdev user,id=vlan${toString nic}"
    ];

in
{
  virtualisation.qemu.options = with pkgs.lib; let
              m = {snd = 1;};
              interfacesNumbered = zipLists vlans (range 1 255);
              interfaces = flip map interfacesNumbered ({ fst, snd }:
                nameValuePair "eth${toString snd}" { ipv4.addresses =
                  [ { address = "192.168.${toString fst}.${toString m.snd}";
                      prefixLength = 24;
                  } ];
                });
    in
      flip map interfacesNumbered
        ({ fst, snd }: qemuNICFlags snd fst m.snd);
}

actually the doc of virtualisation.qemu.vlans is a bit misleading as it will work only with extra code (as in nixos tests). Also I wanted to run my own kernel but qemu-vm hardcodes it. Hopefully qemu uses the last parameter set on the command line so I can still override the nixpkgs one. I might have a look at improving qemu-vm.nix. The pasted code uses SLIRP which seems to be bad for perf. maybe nixos-shell could run some VDE logic as the nixos tests. Anyway that should be good for now on my side.