17

Seems my hosts file (/etc/hosts) points to /nix/store/gds7bha3bx0a22pnzw93pgf0666anpyr-etc-hosts and is read only.

How am I meant to modify this file?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Chris Stryczynski
  • 5,178
  • 5
  • 40
  • 80

2 Answers2

30

Modify the nixos config (usually in /etc/nixos/configuration.nix) with:

networking.extraHosts =
  ''
    127.0.0.2 other-localhost
    10.0.0.1 server
  '';

This is documented at NixOS Wiki and defined here.

Chris Stryczynski
  • 5,178
  • 5
  • 40
  • 80
  • It sounds like it could be an answer; perhaps Chris could use some extra words to explain what this is and why it works? – Jeff Schaller Dec 17 '18 at 16:22
  • 2
    Perhaps a reference could be https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/config/networking.nix#L45 – Jeff Schaller Dec 17 '18 at 16:36
  • When I reviewing, I thought that it looked like a fairly random piece of data. It is clear that is has something to do with the code at the GitHub link that Jeff posted in an earlier comment, but it looks malformed (or I just don't understand the the syntax, which appears different from the GitHub code). If this is a proper answer, please add information about where and how and why this code snipped would be used. Until then, I will still consider this to be a non-answer. If it's a clarification to the question, then please update the question with this info and describe how it's relevant. – Kusalananda Dec 17 '18 at 18:14
  • 1
    Well, yes, `networking.extraHosts` from the `configuration.nix` file will get into the `/etc/hosts` on the resulting NixOS system. – Vladimír Čunát Dec 17 '18 at 18:51
  • 1
    For future travelers, here's what the link above looked like when originally commented: https://github.com/NixOS/nixpkgs/blob/09cbfea2edd8a525f54d96a45c0edc2fe3c94e56/nixos/modules/config/networking.nix#L45 – David Birks Jan 15 '21 at 17:53
  • @Kusalananda the structure at the GitHub link is what defines the option, Chris's example is how to use it. – Steven R. Loomis Mar 28 '23 at 19:08
0

From Readme of nixos-addblock-hosts

nixos-addblock-hosts

A nixos configuration file that you can import from configuration.nix to block common ad/tracker hosts

Download hosts.nix to /etc/nixos/hosts.nix and add it as import to your /etc/nixos/configuration.nix like this (example):

# Edit this configuration file to define what should be installed on
# your system.  Help is available in the configuration.nix(5) man page
# and in the NixOS manual (accessible by running ‘nixos-help’).

{ config, pkgs, ... }:

{
  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix
      ./hosts.nix
    ];

...

Also note this comment from the top of the original hosts file:

# This hosts file is brought to you by Dan Pollock and can be found at
# http://someonewhocares.org/hosts/
# You are free to copy and distribute this file for non-commercial uses,
# as long the original URL and attribution is included. 

PLease also see some github high level code I have no idea about, a chat on discourse.nixos

Tejas Shetty
  • 51
  • 1
  • 7