-
Notifications
You must be signed in to change notification settings - Fork 92
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
Closed
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
33551f5
add userhosts module
bobvanderlinden 26054d7
modules/userhosts: add test
bobvanderlinden aa966d6
modules/userhosts: add documentation
bobvanderlinden 6e7f3cd
add assert for linux
bobvanderlinden dac43f4
trace warning message instead of using assert
bobvanderlinden 71cc52b
only test userhosts on linux
bobvanderlinden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 != {}) { | ||
env = [ | ||
{ | ||
name = "HOSTS_FILE"; | ||
value = "${hostsFile}"; | ||
} | ||
{ | ||
name = "LD_PRELOAD"; | ||
prefix = "${cfg.package}/lib/libuserhosts.so"; | ||
} | ||
]; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
''; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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#L44Is 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 😅There was a problem hiding this comment.
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:
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-usageThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah that makes sense.
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:
Whenever I use
pkgs.stdenv.isLinux
in the if condition of the userhosts module. I don't get this when I replace it withtrue
orfalse
.I also tried adding
, system }:
to the module arguments and usingsystem == "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?There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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.