help-guix
[Top][All Lists]
Advanced

[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:

```scheme
(use-modules
 ((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))) 
tokens))
                      (ip      (car tokens*))
                      (alias   (cadr tokens*)))
                 (host ip alias)))
             lines*)))))

(operating-system
  ...
  (services (cons* (simple-service 'add-extra-hosts hosts-service-type 
(load-hosts-entries))
                   ...
                   %desktop-services)))
```

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
10.1.30.1 name1    # Maybe a comment for myself
10.1.30.2 name2
10.1.30.3 name3
10.1.30.4 name4    # Another comment

# Section Comment 2
10.6.1.1 name5
10.6.1.2 name6     # And yet another comment
10.6.1.3 name7
10.6.1.4 name8
```

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

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!
  Gary

-- 
GPG Key ID: C4FBEDBD
Use `gpg --search-keys tracker@disroot.org' to find me
Protect yourself from surveillance: https://emailselfdefense.fsf.org
=======================================================================
()  ascii ribbon campaign - against html e-mail
/\  www.asciiribbon.org   - against proprietary attachments

Why is HTML email a security nightmare? See https://useplaintext.email/

Please avoid sending me MS-Office attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html



reply via email to

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