cachix / devenv

Fast, Declarative, Reproducible, and Composable Developer Environments
https://devenv.sh
Apache License 2.0
3.56k stars 259 forks source link

hostctl: init module #249

Closed shyim closed 1 year ago

shyim commented 1 year ago

Fixes #227

Config looks like

hosts = {
  "foo" = "127.0.0.1";
};

It will be setup by entering the shell. I did add a proper "caching" so it's calling only hostctl when required because we need to run sudo.

To deactivate, the user has to run deactivate-hosts in the shell and leave the shell. Next opening of the shell readds them

shyim commented 1 year ago

I guess I would add this to devenv up and call the hostctl remove when the command gets canceled using a trap

shyim commented 1 year ago

@domenkozar are you okay with the Idea to add pre and post scripts to devenv up. So we are able to start hostctl and stop it after running the processes. I guess without processes there is not usecase for custom hosts πŸ€”

domenkozar commented 1 year ago

@domenkozar are you okay with the Idea to add pre and post scripts to devenv up. So we are able to start hostctl and stop it after running the processes. I guess without processes there is not usecase for custom hosts thinking

It's still better to always set the hosts, as someone might rely on that as part of the scripts, etc.

shyim commented 1 year ago

The problem is when do we remove the hosts. Currently, I am adding a new command. Feels wrong and can be easily forgotten and then the hosts entries says there πŸ˜….

I would also assume the host entries points to processes running πŸ€”

roberth commented 1 year ago

Not sure what you'd like me to cover. Do you have a nix issue for it?

------- Original Message ------- On Monday, December 26th, 2022 at 12:16, Domen KoΕΎar @.***> wrote:

@.***(https://github.com/roberth) could you cover this one for the next Nix team meeting?

β€” Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

domenkozar commented 1 year ago

Not sure what you'd like me to cover. Do you have a nix issue for it? … ------- Original Message ------- On Monday, December 26th, 2022 at 12:16, Domen KoΕΎar @.> wrote: @.(https://github.com/roberth) could you cover this one for the next Nix team meeting? β€” Reply to this email directly, [view it on GitHub](#249 (comment)), or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

Sorry, I wrote this to the wrong browser tab :)

domenkozar commented 1 year ago

I'm still unsure how to handle the unloading correctly.

Ideally we'd set a trap function in bash and expose that via exitShell for cleanup.

We'd need to fix the whole env loading business, so probably after #191 and #237

shyim commented 1 year ago

Even that would not work. When you have two shells inside it and one leaves the hosts are gone for all πŸ™ˆ

I still somehow like the Idea to couple it with devenv up

domenkozar commented 1 year ago

:+1: let's couple it with devenv up

domenkozar commented 1 year ago

@shyim would be great if you plan to finish :partying_face:

shyim commented 1 year ago

Had no time to look currently in πŸ™ˆ . maybe weekend

bobvanderlinden commented 1 year ago

Nice! Looks good πŸ‘ I was wondering whether an attrset has been considered for configuration?

{
  hosts = {
    "myhost" = "127.0.0.1";
  };
}

So that it is easier to override/remove. Or are there features of /etc/hosts that I'm overlooking?

shyim commented 1 year ago

The only problem is that you want to merge all entries by ip to one line as multiple domains with same IP for some reason slow down DNS resolving on Mac

domenkozar commented 1 year ago

It should still be possible to convert this into the required format with a bit of transformations.

bobvanderlinden commented 1 year ago

This should be able to convert the structure to a hosts file content.

let
  hosts = {
    "myhost" = "127.0.0.1";
    "otherhost" = "127.0.0.1";
    "anotherhost" = "192.168.0.1";
  };
  entries = lib.mapAttrsToList (hostname: ip: { inherit hostname ip; }) hosts;
  entriesByIp = builtins.groupBy ({ ip, ... }: ip) entries;
  hostnamesByIp = builtins.mapAttrs (hostname: entries: builtins.map ({ hostname, ...}: hostname) entries) entriesByIp;
  lines = lib.mapAttrsToList (ip: hostnames: "${ip} ${lib.concatStringsSep " " hostnames}") hostnamesByIp;
in
  lib.concatStringsSep "\n" lines
shyim commented 1 year ago

Sooo I have updated it. First of all @bobvanderlinden thanks <3 it works flawlessly

hosts = {
    "foo" = "127.0.0.1";
  };

processes.test.exec = "ping foo";

That was my example nix file.

image

shyim commented 1 year ago

We can use the same base to support mkcert to add like SSL to caddy

bobvanderlinden commented 1 year ago

Awesome πŸ‘ thanks!