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

modules: introduce userhosts #225

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions extra/services/userhosts.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{ pkgs, config, lib, ... }:
with lib;
let
cfg = config.services.userhosts;
hostsFile = pkgs.writeTextFile {
name = "hosts";
text =
let
lines = lib.mapAttrsToList
(ip: hostnames: "${ip} ${builtins.concatStringsSep " " hostnames}")
cfg.hosts;
in
"${builtins.concatStringsSep "\n" lines}\n";
};
in
{
options.services.userhosts = {
package = mkOption {
type = types.package;
default = pkgs.userhosts;
description = ''
The package containing the LD_PRELOAD library libuserhosts.so.
'';
};
hosts = mkOption {
type = types.attrsOf (types.listOf types.string);
default = {};
description = ''
The host entries to use for userhosts.
The top-level entries are the addresses where hostnames are resolved to.
For each address you can supply a list of hostnames.
This structure represents the structure you'd see in /etc/hosts.

Note that, unlike /etc/hosts, you can also use names to resolve to as well.
'';
example = {
"127.0.0.1" = [ "example.org" ];
"myhost.local" = [ "mydomain.test" ];
};
};
};

config = mkIf (cfg.hosts != {}) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a warning trace if !stdenv.isLinux?

Copy link
Author

Choose a reason for hiding this comment

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

I have attempted to add this, but I can't figure it out.

I attempted the method used in NixOS modules by setting the warnings option, but that option is not (yet?) supported by dev-shell it seems? The commit where I tried is here: https://github.com/bobvanderlinden/devshell/blob/51e8c6bb6f8ab4d3b11acad8953b70f994a45591/extra/services/userhosts.nix#L44
Is it worth the trouble to add a warnings option to dev-shell?

I also attempted to use an assertion. However, somehow it causes an infinite recursion and I couldn't figure out why: https://github.com/bobvanderlinden/devshell/blob/6e7f3cd057082eb76b37a525991992a1946a27e8/extra/services/userhosts.nix#L44
Next to the infinite recursion problem, I thought it wouldn't be ideal using assertion as the test would still fail on MacOS.

In addition, there needs to be some way to skip tests for specific hosts. I haven't found an example in dev-shell yet to do so, do you have some pointers for targeting a test to a single system?

I was contemplating these issues and thought it might be better to work on MacOS support in userhosts instead, as MacOS should be able to do the same thing with DYLD_INSERT_LIBRARIES. That's probably what I'll do next once I get my hands on a OSX machine 😅

Copy link
Member

Choose a reason for hiding this comment

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

Something like that is probably enough:

mkIf (cfg.hosts != {})
if !stdenv.isLinux then
  builtins.trace "warning: userhosts is only available on Linux right now" {}
else
  // rest of the code

I guess NixOS has a warnings option and collects all of them later in the module system, but we don't have that here :)

You can use DYLD_INSERT_LIBRARIES, but I think there are some restrictions and it only works if the macOS system integrity checks are disabled. See https://github.com/jacereda/fsatrace#macos-usage

Copy link
Author

Choose a reason for hiding this comment

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

Ah that makes sense.

You can use DYLD_INSERT_LIBRARIES, but I think there are some restrictions and it only works if the macOS system integrity checks are disabled. See https://github.com/jacereda/fsatrace#macos-usage

Indeed 😢 I ran into this issue working on userhosts for OSX. Oh well, it's fine to be used for Linux for now.

I also made sure the userhosts test is not emitted for non-Linux systems.

That said, I still run into:

error: infinite recursion encountered

       at /nix/store/if23ghw4vi4981dawad0x1vkkkwbmja5-source/lib/modules.nix:469:28:

          468|         builtins.addErrorContext (context name)
          469|           (args.${name} or config._module.args.${name})
             |                            ^
          470|       ) (lib.functionArgs f);

Whenever I use pkgs.stdenv.isLinux in the if condition of the userhosts module. I don't get this when I replace it with true or false.
I also tried adding , system }: to the module arguments and using system == "x86_64-linux", but even that causes infinite loop. I'm quite at a loss why this is happening and I have a hard time debugging this. Any hints on how to continue?

Copy link
Member

Choose a reason for hiding this comment

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

I will go out on a limb and assume that you are using overlays. Stop using overlays!

Typically that would happen if you have your own overlay that has the devshell in it. Then in order to resolve the pkgs attribute, it would have to compute the devshell, and infinite recursion ensues.

Instead do something like this:

let
  sources = import ./nix/sources.nix; # assuming you're using niv
  pkgs = import sources.nixpkgs {};
  devshell = pkgs.callPackage sources.devshell {};
in
devshell.mkShell { ...

Copy link
Author

Choose a reason for hiding this comment

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

From what I can tell, devshell is not in my overlay. I only have a few packages that I created myself in my overlay. Nothing related to devshell.

The CI seems to be running into the same issue.

env = [
{
name = "HOSTS_FILE";
value = "${hostsFile}";
}
{
name = "LD_PRELOAD";
prefix = "${cfg.package}/lib/libuserhosts.so";
}
];
};
}
27 changes: 27 additions & 0 deletions tests/extra/services.userhosts.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{ pkgs, devshell, runTest }:
{
# Basic test
simple =
let
shell = devshell.mkShell {
imports = [ ../../extra/services/userhosts.nix ];
packages = [
pkgs.netcat
];
services.userhosts.hosts = {
"127.0.0.1" = [ "example.org" ];
};
devshell.name = "services-userhosts-simple";
};
in
runTest "simple" { } ''
# Load the devshell
source ${shell}/env.bash

nc -l 127.0.0.1 8080 &
LISTENER_PID=$!
trap "kill $LISTENER_PID 2>/dev/null || true" EXIT
sleep 0.1
nc -zv example.org 8080
'';
}