[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Examples of local-host-entries or hosts-service-type?

From: Gary Johnson
Subject: Re: Examples of local-host-entries or hosts-service-type?
Date: Tue, 21 Feb 2023 10:33:25 -0500

2023/02/10 23:40, Sergiu Ivanov:

> I am reconfiguring my system right now, and guix system reconfigure
> /etc/config.scm tells me this:
> /etc/config.scm:126:27: warning: 'local-host-aliases' is deprecated, use 
> 'local-host-entries' instead
> /etc/config.scm:126:27: warning: 'local-host-aliases' is deprecated, use 
> 'local-host-entries' instead
> /etc/config.scm:124:14: warning: the 'hosts-file' field is deprecated, please 
> use 'hosts-service-type' instead
> For the record, here are the lines guix system reconfigure is
> complaining about:
> (hosts-file (plain-file "hosts"
>                       (string-append
>                        (local-host-aliases host-name)
>                        "some.ip.address.1 machine1\n"
>                        "some.other.ip.address machine2\n")))
> I spent quite some time trying to find some examples of using
> local-host-entries or hosts-service-type, but I don't seem to find any
> mention of these.  Quite on the contrary, the Guix manual actually seems
> to advice declarations similar to those which I have in my
> /etc/config.scm.
> Could someone point me to an example of how I should update
> my configuration?

Hi Sergiu et al,

  I just went through this exercise myself, and I decided to keep my
extra /etc/hosts entries in a separate text file using the original
hosts format. This makes them much easier for me to read and edit. To
provide them to the `hosts-service-type` service, I went ahead and wrote
a little importer function in Scheme that I thought some other folks
might benefit from. Here it is along with how to use it in your
`operating-system` declaration:

 ((gnu services base)    #:select (hosts-service-type host))
 ((gnu services desktop) #:select (%desktop-services))
 ((gnu services)         #:select (simple-service))
 ((gnu system)           #:select (operating-system))
 ((ice-9 ports)          #:select (call-with-input-file))
 ((ice-9 textual-ports)  #:select (get-string-all)))

(define (load-hosts-entries)
  (call-with-input-file "etcfiles/extra-hosts"
    (lambda (p)
      (let* ((text   (get-string-all p))
             (lines  (string-split text #\newline))
             (lines* (filter (lambda (l) (not (or (string-null? l) 
(string-prefix? "#" l)))) lines)))
        (map (lambda (l)
               (let* ((tokens  (string-split l #\space))
                      (tokens* (filter (lambda (t) (not (string-null? t))) 
                      (ip      (car tokens*))
                      (alias   (cadr tokens*)))
                 (host ip alias)))

  (services (cons* (simple-service 'add-extra-hosts hosts-service-type 

To make this work, I have my additional hosts entries in a file called
"etcfiles/extra-hosts" relative to the location of my `system.scm` file
(which contains the `operating-system` declaration above).

That file looks like so:

# Section Comment 1 name1    # Maybe a comment for myself name2 name3 name4    # Another comment

# Section Comment 2 name5 name6     # And yet another comment name7 name8

This is, of course, the regular /etc/hosts format. I just leave out the and ::1 entries for localhost since Guix adds those

Also, please note that the Guix info pages are incorrect for
`hosts-service-type` and `host`. The docs say they are exported by `(gnu
services)`, but they are actually located in `(gnu services base)`. It
would be great if one of the manual maintainers could fix this mistake
as it was one of the trickiest things I had to figure out to make this
code work.

Thanks and happy hacking!

Use `gpg --search-keys' to find me
Protect yourself from surveillance:
()  ascii ribbon campaign - against html e-mail
/\   - against proprietary attachments

Why is HTML email a security nightmare? See

Please avoid sending me MS-Office attachments.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]