Mic92 / nixos-shell

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

Mounting host dirs when running Nixos-Shell with Flake #75

Closed onny closed 9 months ago

onny commented 10 months ago

Hey, I have this flake.nix to launch nixos-shell:

{
  description = "Spawns lightweight nixos vm in a shell";

  inputs = {
    nixpkgs.url = "nixpkgs/nixos-23.11";
    nixos-shell.url = "github:Mic92/nixos-shell";
  };

  outputs = { self, nixpkgs, nixos-shell }: let
    pkgs = nixpkgs.legacyPackages.x86_64-linux;
    start =
      pkgs.writeShellScriptBin "start" ''
        set -e
        export QEMU_NET_OPTS="hostfwd=tcp::8080-:80,hostfwd=tcp::1433-:143,hostfwd=tcp::5877-:587"
        ${pkgs.nixos-shell}/bin/nixos-shell --flake .
       '';
  in {

    nixosConfigurations.vm = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [
        (import ./vm-nextcloud.nix)
        nixos-shell.nixosModules.nixos-shell
      ];
    };

    packages = { inherit start; };
    defaultPackage.x86_64-linux = start;

  };
}

This is great because now the VM itself is pinned to the version of nixpkgs.url defined in this flake.nix.

Inside vm-nextcloud,nix I have this part:

nixos-shell.mounts.extraMounts = {
  "/var/lib/nextcloud/cleanup" = {
     target = ./cleanup;
     cache = "none";
  };
};

But this fails because /var/lib/nextcloud/cleanup is a local path of the host and the Flake cannot access it. I tried to use the --impure flag on nixos-shell but that didn't work.

Best regards Jonas

Mic92 commented 10 months ago

I think with flakes you would need absolute paths. I think we are already relying on --impure to get some other environment variables into nixos-shell

onny commented 9 months ago

Thank you, this was my fault. It's working now like you said :)