Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init project demos #613

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open

Init project demos #613

wants to merge 14 commits into from

Conversation

eljamm
Copy link
Contributor

@eljamm eljamm commented Mar 18, 2025

To test this, you can run:

$ nix-build . -A demo-test
$ ./result

Closes #696

@eljamm eljamm requested review from imincik and erictapen March 18, 2025 09:48
@erictapen
Copy link
Contributor

Nice! The examples don't work yet though, as they lack their module?

$ nix run .#project-demos.Keyoxide/keyoxide-web
error:
       … while evaluating the attribute 'config.system.build.vm'
         at /nix/store/crrwsv142k1vkwdba7q26y73760n4bll-source/lib/modules.nix:336:9:
          335|         options = checked options;
          336|         config = checked (removeAttrs config [ "_module" ]);
             |         ^
          337|         _module = checked (config._module);

       … while calling the 'seq' builtin
         at /nix/store/crrwsv142k1vkwdba7q26y73760n4bll-source/lib/modules.nix:336:18:
          335|         options = checked options;
          336|         config = checked (removeAttrs config [ "_module" ]);
             |                  ^
          337|         _module = checked (config._module);

       (stack trace truncated; use '--show-trace' to show the full, detailed trace)

       error: The option `services.keyoxide' does not exist. Definition values:
       - In `/nix/store/y54pzyxbd1bdf8i4pzg1yvq0q7q0jl24-source/projects/Keyoxide/keyoxide-web/example.nix':
           {
             enable = true;
           }

@eljamm
Copy link
Contributor Author

eljamm commented Mar 18, 2025

Yeah, let's just keep importing all the modules for now. The example in the description is probably not the best either. I'll change that in a second.

@eljamm
Copy link
Contributor Author

eljamm commented Mar 18, 2025

One thing I'm thinking about is that we could probably make 2 types of VMs depending on what the example needs:

  • Graphical with GPU acceleration (OpenGL or Vulkan)
  • Minimal CLI only

Or we could run everything in a graphical VM and have the experience be close to what it feels like using NixOS as a users.

@erictapen
Copy link
Contributor

Yeah I was thinking about how much customisation each demo VM might need. My tendency would be to keep stuff as minimalistic as possible, e.g. a service that mostly provides a web interface should not boot a display manager.

@fricklerhandwerk
Copy link
Contributor

Yes, another heuristic is to look at the intended use case, one of which is self hosting web services -- we don't need a GUI there.

@eljamm eljamm marked this pull request as draft March 21, 2025 09:49
@eljamm eljamm force-pushed the init-project-demos branch from 3322e25 to b312b4e Compare April 2, 2025 14:49
@eljamm
Copy link
Contributor Author

eljamm commented Apr 2, 2025

Fully contained Cryptpad demo, with ssh and service ports forwarded to the host:

# default.nix
{
  ngipkgs ?
    import
      (fetchTarball "https://github.com/eljamm/ngipkgs/tarball/init-project-demos/8eb7f038fd62fd6490e017d78e6a30e7b64a13fa")
      { },
}:
let
  servicePort = 9000;
  domainName = "localhost:${toString servicePort}";
in
ngipkgs.demo {
  services.cryptpad = {
    enable = true;
    settings = {
      httpPort = servicePort;
      httpAddress = "0.0.0.0";
      httpUnsafeOrigin = "http://${domainName}";
      httpSafeOrigin = "http://${domainName}";
    };
  };

  networking.firewall.allowedTCPPorts = [ servicePort ];
  networking.firewall.allowedUDPPorts = [ servicePort ];
}
$ nix-build
$ ./resullt

@eljamm eljamm self-assigned this Apr 2, 2025
@eljamm eljamm marked this pull request as ready for review April 2, 2025 15:17
memorySize = 4096;
cores = 4;
graphics = false;
diskImage = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a disk image is useful because it allows you to work with larger data and it stores the state between multiple VM runs (data, shell history). Disadvantage is that it creates disk image file on your hard drive (qcow2 file).

Suggested change
diskImage = null;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • When diskImage is not disabled, root fs is mounted to real disk image:
/dev/vda on / type ext4 (rw,relatime)
  • When diskImage = null;, root fs is mounted to tmpfs which is VM's memory
tmpfs on / type tmpfs (rw,relatime,mode=755)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When diskImage = null;, root fs is mounted to tmpfs which is VM's memory

So I imagine this would be a problem for examples that require a lot of space, correct?


services.openssh = {
enable = true;
ports = lib.mkDefault [ 2222 ];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep default port value.

Suggested change
ports = lib.mkDefault [ 2222 ];

Copy link
Contributor Author

@eljamm eljamm Apr 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default won't work if the ssh server is already running on your machine since that would cause a conflict, as you mentioned in your other comment:

Could not set up host forwarding rule 'tcp::22-:22'

I think 2222 is a sane default, which can still be overwritten by the users is they wish. We just need to document this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to keep all port numbers standard/default on guest machine to avoid confusion.

SSH can run on port 22 on guest machine. But it needs to be forwarded to some other value on host - for example 10022.

We can use following port forwarding formula:

  • if guest port number is < 100 then host port number = 100
  • if guest port number is 100 - 1000 and then host port number = 10
  • if guest port number is > 1000 and then host port number = 1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SSH can run on port 22 on guest machine. But it needs to be forwarded to some other value on host - for example 10022.

How would the user change it in this case?

We can use following port forwarding formula:

I'm not sure I follow the logic here. If we have 2 guest ports: 20 and 30, then they'll both be mapped to 100 according to the formula.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Further more, it could also be odd or confusing for users to set up a port and find it mapped to another and I don't see us touching any other non-service example ports, aside from ssh.

Copy link
Contributor Author

@eljamm eljamm Apr 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, if this is really an issue, I think it would be sufficient to map each guest port to port + 10000 in the host and add that as a note in the instructions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, if this is really an issue, I think it would be sufficient to map each guest port to port + 10000 in the host and add that as a note in the instructions.

Yes, I like this idea.

"-enable-kvm"
];

# ssh + open service ports
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ports can't be forwarded to the same value to the host. This might cause conflict on host or might not be permitted (forwarding ports under 1024).

Copy link
Contributor Author

@eljamm eljamm Apr 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a service port is not available on the host, users can just change it from the config to something that is and that will be forwarded instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In progress
Development

Successfully merging this pull request may close these issues.

Implement demo function in default.nix
4 participants