-
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 hostctl (static dns for development) #75
Open
blaggacao
wants to merge
3
commits into
numtide:main
Choose a base branch
from
blaggacao:da/hosctl-mod
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,61 @@ | ||
{ lib, pkgs, config, ... }: | ||
with lib; | ||
let | ||
cfg = config.hostctl; | ||
profile = toLower config.devshell.name; | ||
|
||
etcHosts = pkgs.writeText "${profile}-etchosts" ( | ||
concatStringsSep "\n" | ||
(mapAttrsToList (host: ip: ip + " " + host) cfg.dns) | ||
); | ||
|
||
# Execute this script to install the project's static dns entries | ||
install-hostctl-dns = pkgs.writeShellScriptBin "install-hostctl-dns" '' | ||
set -euo pipefail | ||
shopt -s nullglob | ||
|
||
log() { | ||
IFS=$'\n' loglines=($*) | ||
for line in ${"$"}{loglines[@]}; do echo -e "[hostctl] $line" >&2; done | ||
} | ||
|
||
# Install local CA into system, java and nss (includes Firefox) trust stores | ||
log "Update static dns entries..." | ||
sudo -K | ||
log $(sudo ${pkgs.hostctl}/bin/hostctl add ${profile} --from ${etcHosts} 2>&1) | ||
|
||
uninstall() { | ||
log $(sudo ${pkgs.hostctl}/bin/hostctl remove ${profile} 2>&1) | ||
} | ||
|
||
# TODO: Uninstall when leaving the devshell | ||
# trap uninstall EXIT | ||
|
||
''; | ||
in | ||
{ | ||
options.hostctl = { | ||
enable = mkEnableOption "manage temporary /etc/host entries for development from within the shell"; | ||
|
||
dns = mkOption { | ||
type = types.attrs; | ||
default = {}; | ||
description = "configure static dns entries"; | ||
example = | ||
{ | ||
dns."some.host" = "1.2.3.4"; | ||
dns."another.host" = "4.3.2.1"; | ||
}; | ||
}; | ||
}; | ||
|
||
config = mkIf cfg.enable { | ||
commands = [ { package = pkgs.hostctl; category = "dns"; } ]; | ||
devshell = { | ||
packages = [ install-hostctl-dns ]; | ||
startup.install-hostctl-dns.text = " | ||
$DEVSHELL_DIR/bin/install-hostctl-dns | ||
"; | ||
}; | ||
}; | ||
} |
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.
This seems hard to get right, particularly because when the user runs two dev-shells and exits one.
Modifying the system is a bit heavy-handed for a development shell. I was secretly hoping for an unprivileged solution when I saw this PR. Perhaps (userns) containers are a more appropriate solution for most use cases.
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.
There is indeed a long term heavily politically involved solution to this: https://github.com/apetresc/nss-hostslocal. That would require lobbying for changing the way how linux system generally resolve hostfiles these days.
This particular PR is not going to get merged for this reason.
However, in a dev team setting, where you can exert certain powers over an individual's machine, this is a solution that (somewhat) works.