Nixos-shell: Difference between revisions

imported>Onny
mNo edit summary
imported>Onny
Add Flakes example
Line 16: Line 16:
{{file|myvm.nix|nix|<nowiki>
{{file|myvm.nix|nix|<nowiki>
{ pkgs, ... }: {
{ pkgs, ... }: {
   services.dokuwiki.sites."localhost" = {
   services.dokuwiki.sites."localhost" = {
     enable = true;
     enable = true;
Line 48: Line 47:


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 ===
Using following [[Flakes]] example, you can start a virtual machine using nixos-shell by just typing <code>nix run</code>
{{file|flake.nix|nix|<nowiki>
{
  description = "Spawns lightweight nixos vm in a shell";
  inputs.nixpkgs.url = "nixpkgs/nixos-23.11";
  outputs = { self, nixpkgs }: let
    pkgs = nixpkgs.legacyPackages.x86_64-linux;
    start =
      pkgs.writeShellScriptBin "start" ''
        set -e
        export QEMU_NET_OPTS="hostfwd=tcp::8080-:80"
        ${pkgs.nixos-shell}/bin/nixos-shell --flake .
      '';
  in {
    nixosConfigurations.vm = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [
        (import ./myvm.nix)
        self.nixosModules.nixos-shell
      ];
    };
    nixosModules.nixos-shell = import ./share/modules/nixos-shell.nix;
    packages = { inherit start; };
    defaultPackage.x86_64-linux = start;
  };
}
</nowiki>}}
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.