mirror of
https://github.com/nix-community/nix-on-droid.git
synced 2025-11-08 19:46:07 +01:00
modules/environment/networking: implement /etc/hosts options
This commit is contained in:
parent
2301e01d48
commit
b324ef2824
6 changed files with 143 additions and 6 deletions
1
AUTHORS
1
AUTHORS
|
|
@ -1,3 +1,4 @@
|
||||||
Alexander Sosedkin <monk@unboiled.info>
|
Alexander Sosedkin <monk@unboiled.info>
|
||||||
Tobias Happ <tobias.happ@gmx.de>
|
Tobias Happ <tobias.happ@gmx.de>
|
||||||
Bruno Bigras <bigras.bruno@gmail.com>
|
Bruno Bigras <bigras.bruno@gmail.com>
|
||||||
|
Evgeny Kurnevsky <kurnevsky@gmail.com>
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,11 @@
|
||||||
|
|
||||||
## Release 23.11 (unreleased)
|
## Release 23.11 (unreleased)
|
||||||
|
|
||||||
|
### New Options
|
||||||
|
|
||||||
|
* New options `networking.hosts`, `networking.hostFiles` and
|
||||||
|
`networking.extraHosts` for `/etc/hosts` configuration.
|
||||||
|
|
||||||
## Release 23.05
|
## Release 23.05
|
||||||
|
|
||||||
### New Options
|
### New Options
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,91 @@
|
||||||
# Copyright (c) 2019-2021, see AUTHORS. Licensed under MIT License, see LICENSE.
|
# Copyright (c) 2019-2023, see AUTHORS. Licensed under MIT License, see LICENSE.
|
||||||
|
|
||||||
|
# Inspired by
|
||||||
|
# https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/config/networking.nix
|
||||||
|
# (Copyright (c) 2003-2023 Eelco Dolstra and the Nixpkgs/NixOS contributors,
|
||||||
|
# licensed under MIT License as well)
|
||||||
|
|
||||||
{ config, lib, pkgs, ... }:
|
{ config, lib, pkgs, ... }:
|
||||||
|
|
||||||
with lib;
|
with lib;
|
||||||
|
|
||||||
|
let
|
||||||
|
cfg = config.networking;
|
||||||
|
|
||||||
|
localhostMultiple = any (elem "localhost") (attrValues (removeAttrs cfg.hosts [ "127.0.0.1" "::1" ]));
|
||||||
|
in
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
||||||
###### interface
|
###### interface
|
||||||
|
|
||||||
options = { };
|
options = {
|
||||||
|
|
||||||
|
networking.hosts = lib.mkOption {
|
||||||
|
type = types.attrsOf (types.listOf types.str);
|
||||||
|
default = { };
|
||||||
|
example = literalExpression ''
|
||||||
|
{
|
||||||
|
"127.0.0.1" = [ "foo.bar.baz" ];
|
||||||
|
"192.168.0.2" = [ "fileserver.local" "nameserver.local" ];
|
||||||
|
};
|
||||||
|
'';
|
||||||
|
description = lib.mdDoc ''
|
||||||
|
Locally defined maps of hostnames to IP addresses.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
|
networking.hostFiles = lib.mkOption {
|
||||||
|
type = types.listOf types.path;
|
||||||
|
defaultText = literalMD "Hosts from {option}`networking.hosts` and {option}`networking.extraHosts`";
|
||||||
|
example = literalExpression ''[ "''${pkgs.my-blocklist-package}/share/my-blocklist/hosts" ]'';
|
||||||
|
description = lib.mdDoc ''
|
||||||
|
Files that should be concatenated together to form {file}`/etc/hosts`.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
|
networking.extraHosts = lib.mkOption {
|
||||||
|
type = types.lines;
|
||||||
|
default = "";
|
||||||
|
example = "192.168.0.1 lanlocalhost";
|
||||||
|
description = lib.mdDoc ''
|
||||||
|
Additional verbatim entries to be appended to {file}`/etc/hosts`.
|
||||||
|
For adding hosts from derivation results, use {option}`networking.hostFiles` instead.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
###### implementation
|
###### implementation
|
||||||
|
|
||||||
config = {
|
config = {
|
||||||
|
|
||||||
|
assertions = [{
|
||||||
|
assertion = !localhostMultiple;
|
||||||
|
message = ''
|
||||||
|
`networking.hosts` maps "localhost" to something other than "127.0.0.1"
|
||||||
|
or "::1". This will break some applications. Please use
|
||||||
|
`networking.extraHosts` if you really want to add such a mapping.
|
||||||
|
'';
|
||||||
|
}];
|
||||||
|
|
||||||
|
networking.hostFiles =
|
||||||
|
let
|
||||||
|
localhostHosts = pkgs.writeText "localhost-hosts" ''
|
||||||
|
127.0.0.1 localhost
|
||||||
|
::1 localhost
|
||||||
|
'';
|
||||||
|
stringHosts =
|
||||||
|
let
|
||||||
|
oneToString = set: ip: ip + " " + concatStringsSep " " set.${ip} + "\n";
|
||||||
|
allToString = set: concatMapStrings (oneToString set) (attrNames set);
|
||||||
|
in
|
||||||
|
pkgs.writeText "string-hosts" (allToString (filterAttrs (_: v: v != [ ]) cfg.hosts));
|
||||||
|
extraHosts = pkgs.writeText "extra-hosts" cfg.extraHosts;
|
||||||
|
in
|
||||||
|
mkBefore [ localhostHosts stringHosts extraHosts ];
|
||||||
|
|
||||||
environment.etc = {
|
environment.etc = {
|
||||||
# /etc/services: TCP/UDP port assignments.
|
# /etc/services: TCP/UDP port assignments.
|
||||||
services.source = pkgs.iana-etc + "/etc/services";
|
services.source = pkgs.iana-etc + "/etc/services";
|
||||||
|
|
@ -23,10 +94,7 @@ with lib;
|
||||||
protocols.source = pkgs.iana-etc + "/etc/protocols";
|
protocols.source = pkgs.iana-etc + "/etc/protocols";
|
||||||
|
|
||||||
# /etc/hosts: Hostname-to-IP mappings.
|
# /etc/hosts: Hostname-to-IP mappings.
|
||||||
hosts.text = ''
|
hosts.source = pkgs.concatText "hosts" cfg.hostFiles;
|
||||||
127.0.0.1 localhost
|
|
||||||
::1 localhost
|
|
||||||
'';
|
|
||||||
|
|
||||||
"resolv.conf".text = ''
|
"resolv.conf".text = ''
|
||||||
nameserver 1.1.1.1
|
nameserver 1.1.1.1
|
||||||
|
|
|
||||||
7
tests/on-device/config-flake-hosts-localhost.cfg.nix
Normal file
7
tests/on-device/config-flake-hosts-localhost.cfg.nix
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
{ pkgs, ... }:
|
||||||
|
|
||||||
|
{
|
||||||
|
system.stateVersion = "23.05";
|
||||||
|
|
||||||
|
networking.hosts."127.0.0.2" = [ "localhost" ];
|
||||||
|
}
|
||||||
37
tests/on-device/config-flake-hosts.bats
Normal file
37
tests/on-device/config-flake-hosts.bats
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
# Copyright (c) 2023, see AUTHORS. Licensed under MIT License, see LICENSE.
|
||||||
|
|
||||||
|
load lib
|
||||||
|
|
||||||
|
@test 'hosts can be configured' {
|
||||||
|
# set up / build / activate the configuration
|
||||||
|
cat "$ON_DEVICE_TESTS_DIR/config-flake-hosts.cfg.nix" \
|
||||||
|
> ~/.config/nixpkgs/nix-on-droid.nix
|
||||||
|
_sed "s|<<FLAKE_URL>>|$FLAKE_URL|g" \
|
||||||
|
"$ON_DEVICE_TESTS_DIR/config-flake.nix" \
|
||||||
|
> ~/.config/nixpkgs/flake.nix
|
||||||
|
|
||||||
|
nix-on-droid switch --flake ~/.config/nixpkgs#device
|
||||||
|
|
||||||
|
# check that /etc/hosts contains configured hosts
|
||||||
|
for entry in '::1 localhost' \
|
||||||
|
'127.0.0.1 localhost' \
|
||||||
|
'127.0.0.2 a b' \
|
||||||
|
'127.0.0.3 c' \
|
||||||
|
'127.0.0.4 d'
|
||||||
|
do
|
||||||
|
grep "$entry" /etc/hosts
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
@test 'hosts can not map localhost' {
|
||||||
|
# set up / build / activate the configuration
|
||||||
|
cat "$ON_DEVICE_TESTS_DIR/config-flake-hosts-localhost.cfg.nix" \
|
||||||
|
> ~/.config/nixpkgs/nix-on-droid.nix
|
||||||
|
_sed "s|<<FLAKE_URL>>|$FLAKE_URL|g" \
|
||||||
|
"$ON_DEVICE_TESTS_DIR/config-flake.nix" \
|
||||||
|
> ~/.config/nixpkgs/flake.nix
|
||||||
|
|
||||||
|
# check that networking.hosts can't map localhost
|
||||||
|
run nix-on-droid switch --flake ~/.config/nixpkgs#device
|
||||||
|
[ "$status" -eq 1 ]
|
||||||
|
}
|
||||||
19
tests/on-device/config-flake-hosts.cfg.nix
Normal file
19
tests/on-device/config-flake-hosts.cfg.nix
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
{ pkgs, ... }:
|
||||||
|
|
||||||
|
{
|
||||||
|
system.stateVersion = "23.05";
|
||||||
|
|
||||||
|
networking = {
|
||||||
|
hosts."127.0.0.2" = [ "a" "b" ];
|
||||||
|
|
||||||
|
extraHosts = ''
|
||||||
|
127.0.0.3 c
|
||||||
|
'';
|
||||||
|
|
||||||
|
hostFiles = [
|
||||||
|
(pkgs.writeText "hosts" ''
|
||||||
|
127.0.0.4 d
|
||||||
|
'')
|
||||||
|
];
|
||||||
|
};
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue