23

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?

2 Answers 2

37

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.

6
  • 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
    Commented Dec 17, 2018 at 16:22
  • 2
    Perhaps a reference could be github.com/NixOS/nixpkgs/blob/master/nixos/modules/config/…
    – Jeff Schaller
    Commented Dec 17, 2018 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
    Commented Dec 17, 2018 at 18:14
  • 1
    Well, yes, networking.extraHosts from the configuration.nix file will get into the /etc/hosts on the resulting NixOS system. Commented Dec 17, 2018 at 18:51
  • 1
    For future travelers, here's what the link above looked like when originally commented: github.com/NixOS/nixpkgs/blob/… Commented Jan 15, 2021 at 17:53
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .