Nixos-shell: Difference between revisions

imported>Onny
Add Flakes example
Onny (talk | contribs)
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
[https://github.com/Mic92/nixos-shell Nixos-shell] is a small helper script for spawning lightweight NixOS virtual machines in a shell.
{{DISPLAYTITLE:{{#if:{{NAMESPACE}}|{{NAMESPACE}}:|}}{{lcfirst:{{PAGENAME}}}}}}
[https://github.com/Mic92/nixos-shell nixos-shell] is a small helper script for spawning lightweight NixOS virtual machines in a shell.


== Installation ==
== Installation ==
Line 20: Line 21:
     settings.title = "My Wiki";
     settings.title = "My Wiki";
   };
   };
};
}
</nowiki>}}
</nowiki>}}


Line 30: Line 31:


After the VM is successfully booted, DokuWiki will be available on http://localhost:8080
After the VM is successfully booted, DokuWiki will be available on http://localhost:8080
=== Reference local nixpkgs folder ===
Using the <code>-I nixpkgs</code> parameter, you could choose to use a local ''nixpkgs'' repository, for example to test unfinished packages or modules:<syntaxhighlight lang="bash">
nixos-shell -I nixpkgs=/home/myuser/projects/nixpkgs myvm.nix
</syntaxhighlight>
=== Graphical session ===
Following snippet will spawn a QEMU session with a graphical screen running GNOME, configured to auto login the user <code>nixos</code>.{{file|myvm.nix|nix|<nowiki>
{ ... }: {
  virtualisation.memorySize = 8096;
  virtualisation.cores = 8;
  virtualisation.graphics = true;
  services.displayManager.gdm.enable = true;
  services.desktopManager.gnome.enable = true;
  services.displayManager.autoLogin = {
    enable = true;
    user = "nixos";
  };
  users.users.nixos = {
    isNormalUser = true;
    initialPassword = "nixos";
  };
};
</nowiki>|name=|lang=}}


=== Mounting host directories ===
=== Mounting host directories ===
Line 36: Line 65:


{{file|myvm.nix|nix|<nowiki>
{{file|myvm.nix|nix|<nowiki>
{ pkgs, ... }: {
{ ... }: {
   nixos-shell.mounts.extraMounts = {
   nixos-shell.mounts.extraMounts = {
     "/var/lib/nextcloud/store-apps/calendar" = {
     "/var/lib/nextcloud/store-apps/calendar" = {
Line 44: Line 73:
   };
   };
};
};
</nowiki>}}
</nowiki>|name=|lang=}}


Mounting is done through the network filesystem protocol 9p. Currently [https://github.com/Mic92/nixos-shell/issues/71 it's not possible] to mount the target directory with a specific UID/GID, so you'll have to change the permissions on the host directory according to your needs.
Mounting is done through the network filesystem protocol 9p. Currently [https://github.com/Mic92/nixos-shell/issues/71 it's not possible] to mount the target directory with a specific UID/GID, so you'll have to change the permissions on the host directory according to your needs.


=== Using inside Flake ===
=== Inside Nix Flake ===


Using following [[Flakes]] example, you can start a virtual machine using nixos-shell by just typing <code>nix run</code>
Using following [[Flakes]] example, you can start a virtual machine using nixos-shell by just typing <code>nix run</code>
Line 56: Line 85:
   description = "Spawns lightweight nixos vm in a shell";
   description = "Spawns lightweight nixos vm in a shell";


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


   outputs = { self, nixpkgs }: let
   outputs = { self, nixpkgs, nixos-shell }: let
     pkgs = nixpkgs.legacyPackages.x86_64-linux;
     pkgs = nixpkgs.legacyPackages.x86_64-linux;
     start =
     start =
Line 72: Line 104:
       modules = [
       modules = [
         (import ./myvm.nix)
         (import ./myvm.nix)
         self.nixosModules.nixos-shell
         nixos-shell.nixosModules.nixos-shell
       ];
       ];
     };
     };
    nixosModules.nixos-shell = import ./share/modules/nixos-shell.nix;


     packages = { inherit start; };
     packages = { inherit start; };
Line 86: Line 116:


The configuration of the virtual machine is inside the file <code>myvm.nix</code> in the same directory. The virtual machine will use the nixpkgs source defined in the flake inputs.
The configuration of the virtual machine is inside the file <code>myvm.nix</code> in the same directory. The virtual machine will use the nixpkgs source defined in the flake inputs.
[[Category:Container]]