networking.scm 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
  4. ;;; Copyright © 2016, 2018, 2020 Efraim Flashner <efraim@flashner.co.il>
  5. ;;; Copyright © 2016 John Darrington <jmd@gnu.org>
  6. ;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
  7. ;;; Copyright © 2017 Thomas Danckaert <post@thomasdanckaert.be>
  8. ;;; Copyright © 2017, 2018 Marius Bakke <mbakke@fastmail.com>
  9. ;;; Copyright © 2018 Tobias Geerinckx-Rice <me@tobias.gr>
  10. ;;; Copyright © 2018 Chris Marusich <cmmarusich@gmail.com>
  11. ;;; Copyright © 2018 Arun Isaac <arunisaac@systemreboot.net>
  12. ;;; Copyright © 2019 Florian Pelz <pelzflorian@pelzflorian.de>
  13. ;;; Copyright © 2019, 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  14. ;;; Copyright © 2019 Sou Bunnbu <iyzsong@member.fsf.org>
  15. ;;; Copyright © 2019 Alex Griffin <a@ajgrf.com>
  16. ;;; Copyright © 2020 Brice Waegeneire <brice@waegenei.re>
  17. ;;; Copyright © 2021 Oleg Pykhalov <go.wigust@gmail.com>
  18. ;;; Copyright © 2021 Christine Lemmer-Webber <cwebber@dustycloud.org>
  19. ;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
  20. ;;; Copyright © 2021 Guillaume Le Vaillant <glv@posteo.net>
  21. ;;;
  22. ;;; This file is part of GNU Guix.
  23. ;;;
  24. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  25. ;;; under the terms of the GNU General Public License as published by
  26. ;;; the Free Software Foundation; either version 3 of the License, or (at
  27. ;;; your option) any later version.
  28. ;;;
  29. ;;; GNU Guix is distributed in the hope that it will be useful, but
  30. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  31. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. ;;; GNU General Public License for more details.
  33. ;;;
  34. ;;; You should have received a copy of the GNU General Public License
  35. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  36. (define-module (gnu services networking)
  37. #:use-module (gnu services)
  38. #:use-module (gnu services base)
  39. #:use-module (gnu services configuration)
  40. #:use-module (gnu services linux)
  41. #:use-module (gnu services shepherd)
  42. #:use-module (gnu services dbus)
  43. #:use-module (gnu system shadow)
  44. #:use-module (gnu system pam)
  45. #:use-module (gnu packages admin)
  46. #:use-module (gnu packages base)
  47. #:use-module (gnu packages bash)
  48. #:use-module (gnu packages cluster)
  49. #:use-module (gnu packages connman)
  50. #:use-module (gnu packages freedesktop)
  51. #:use-module (gnu packages linux)
  52. #:use-module (gnu packages tor)
  53. #:use-module (gnu packages usb-modeswitch)
  54. #:use-module (gnu packages messaging)
  55. #:use-module (gnu packages networking)
  56. #:use-module (gnu packages ntp)
  57. #:use-module (gnu packages wicd)
  58. #:use-module (gnu packages gnome)
  59. #:use-module (gnu packages ipfs)
  60. #:use-module (gnu build linux-container)
  61. #:use-module (guix gexp)
  62. #:use-module (guix records)
  63. #:use-module (guix modules)
  64. #:use-module (guix packages)
  65. #:use-module (guix deprecation)
  66. #:use-module (rnrs enums)
  67. #:use-module (srfi srfi-1)
  68. #:use-module (srfi srfi-9)
  69. #:use-module (srfi srfi-26)
  70. #:use-module (srfi srfi-43)
  71. #:use-module (ice-9 match)
  72. #:use-module (json)
  73. #:re-export (static-networking-service
  74. static-networking-service-type)
  75. #:export (%facebook-host-aliases
  76. dhcp-client-service-type
  77. dhcpd-service-type
  78. dhcpd-configuration
  79. dhcpd-configuration?
  80. dhcpd-configuration-package
  81. dhcpd-configuration-config-file
  82. dhcpd-configuration-version
  83. dhcpd-configuration-run-directory
  84. dhcpd-configuration-lease-file
  85. dhcpd-configuration-pid-file
  86. dhcpd-configuration-interfaces
  87. ntp-configuration
  88. ntp-configuration?
  89. ntp-configuration-ntp
  90. ntp-configuration-servers
  91. ntp-allow-large-adjustment?
  92. %ntp-servers
  93. ntp-server
  94. ntp-server-type
  95. ntp-server-address
  96. ntp-server-options
  97. ntp-service-type
  98. %openntpd-servers
  99. openntpd-configuration
  100. openntpd-configuration?
  101. openntpd-service-type
  102. inetd-configuration
  103. inetd-entry
  104. inetd-service-type
  105. opendht-configuration
  106. opendht-configuration-peer-discovery?
  107. opendht-configuration-verbose?
  108. opendht-configuration-bootstrap-host
  109. opendht-configuration-port
  110. opendht-configuration-proxy-server-port
  111. opendht-configuration-proxy-server-port-tls
  112. opendht-configuration->command-line-arguments
  113. opendht-shepherd-service
  114. opendht-service-type
  115. tor-configuration
  116. tor-configuration?
  117. tor-hidden-service
  118. tor-service-type
  119. wicd-service-type
  120. wicd-service
  121. network-manager-configuration
  122. network-manager-configuration?
  123. network-manager-configuration-dns
  124. network-manager-configuration-vpn-plugins
  125. network-manager-service-type
  126. connman-configuration
  127. connman-configuration?
  128. connman-service-type
  129. modem-manager-configuration
  130. modem-manager-configuration?
  131. modem-manager-service-type
  132. usb-modeswitch-configuration
  133. usb-modeswitch-configuration?
  134. usb-modeswitch-configuration-usb-modeswitch
  135. usb-modeswitch-configuration-usb-modeswitch-data
  136. usb-modeswitch-service-type
  137. wpa-supplicant-configuration
  138. wpa-supplicant-configuration?
  139. wpa-supplicant-configuration-wpa-supplicant
  140. wpa-supplicant-configuration-requirement
  141. wpa-supplicant-configuration-pid-file
  142. wpa-supplicant-configuration-dbus?
  143. wpa-supplicant-configuration-interface
  144. wpa-supplicant-configuration-config-file
  145. wpa-supplicant-configuration-extra-options
  146. wpa-supplicant-service-type
  147. hostapd-configuration
  148. hostapd-configuration?
  149. hostapd-configuration-package
  150. hostapd-configuration-interface
  151. hostapd-configuration-ssid
  152. hostapd-configuration-broadcast-ssid?
  153. hostapd-configuration-channel
  154. hostapd-configuration-driver
  155. hostapd-service-type
  156. simulated-wifi-service-type
  157. openvswitch-service-type
  158. openvswitch-configuration
  159. iptables-configuration
  160. iptables-configuration?
  161. iptables-configuration-iptables
  162. iptables-configuration-ipv4-rules
  163. iptables-configuration-ipv6-rules
  164. iptables-service-type
  165. nftables-service-type
  166. nftables-configuration
  167. nftables-configuration?
  168. nftables-configuration-package
  169. nftables-configuration-ruleset
  170. %default-nftables-ruleset
  171. pagekite-service-type
  172. pagekite-configuration
  173. pagekite-configuration?
  174. pagekite-configuration-package
  175. pagekite-configuration-kitename
  176. pagekite-configuration-kitesecret
  177. pagekite-configuration-frontend
  178. pagekite-configuration-kites
  179. pagekite-configuration-extra-file
  180. yggdrasil-service-type
  181. yggdrasil-configuration
  182. yggdrasil-configuration?
  183. yggdrasil-configuration-autoconf?
  184. yggdrasil-configuration-config-file
  185. yggdrasil-configuration-log-level
  186. yggdrasil-configuration-log-to
  187. yggdrasil-configuration-json-config
  188. yggdrasil-configuration-package
  189. ipfs-service-type
  190. ipfs-configuration
  191. ipfs-configuration?
  192. ipfs-configuration-package
  193. ipfs-configuration-gateway
  194. ipfs-configuration-api
  195. keepalived-configuration
  196. keepalived-configuration?
  197. keepalived-service-type))
  198. ;;; Commentary:
  199. ;;;
  200. ;;; Networking services.
  201. ;;;
  202. ;;; Code:
  203. (define %facebook-host-aliases
  204. ;; This is the list of known Facebook hosts to be added to /etc/hosts if you
  205. ;; are to block it.
  206. "\
  207. # Block Facebook IPv4.
  208. 127.0.0.1 www.facebook.com
  209. 127.0.0.1 facebook.com
  210. 127.0.0.1 login.facebook.com
  211. 127.0.0.1 www.login.facebook.com
  212. 127.0.0.1 fbcdn.net
  213. 127.0.0.1 www.fbcdn.net
  214. 127.0.0.1 fbcdn.com
  215. 127.0.0.1 www.fbcdn.com
  216. 127.0.0.1 static.ak.fbcdn.net
  217. 127.0.0.1 static.ak.connect.facebook.com
  218. 127.0.0.1 connect.facebook.net
  219. 127.0.0.1 www.connect.facebook.net
  220. 127.0.0.1 apps.facebook.com
  221. # Block Facebook IPv6.
  222. fe80::1%lo0 facebook.com
  223. fe80::1%lo0 login.facebook.com
  224. fe80::1%lo0 www.login.facebook.com
  225. fe80::1%lo0 fbcdn.net
  226. fe80::1%lo0 www.fbcdn.net
  227. fe80::1%lo0 fbcdn.com
  228. fe80::1%lo0 www.fbcdn.com
  229. fe80::1%lo0 static.ak.fbcdn.net
  230. fe80::1%lo0 static.ak.connect.facebook.com
  231. fe80::1%lo0 connect.facebook.net
  232. fe80::1%lo0 www.connect.facebook.net
  233. fe80::1%lo0 apps.facebook.com\n")
  234. (define dhcp-client-service-type
  235. (shepherd-service-type
  236. 'dhcp-client
  237. (lambda (dhcp)
  238. (define dhclient
  239. (file-append dhcp "/sbin/dhclient"))
  240. (define pid-file
  241. "/var/run/dhclient.pid")
  242. (shepherd-service
  243. (documentation "Set up networking via DHCP.")
  244. (requirement '(user-processes udev))
  245. ;; XXX: Running with '-nw' ("no wait") avoids blocking for a minute when
  246. ;; networking is unavailable, but also means that the interface is not up
  247. ;; yet when 'start' completes. To wait for the interface to be ready, one
  248. ;; should instead monitor udev events.
  249. (provision '(networking))
  250. (start #~(lambda _
  251. ;; When invoked without any arguments, 'dhclient' discovers all
  252. ;; non-loopback interfaces *that are up*. However, the relevant
  253. ;; interfaces are typically down at this point. Thus we perform
  254. ;; our own interface discovery here.
  255. (define valid?
  256. (lambda (interface)
  257. (and (arp-network-interface? interface)
  258. (not (loopback-network-interface? interface))
  259. ;; XXX: Make sure the interfaces are up so that
  260. ;; 'dhclient' can actually send/receive over them.
  261. ;; Ignore those that cannot be activated.
  262. (false-if-exception
  263. (set-network-interface-up interface)))))
  264. (define ifaces
  265. (filter valid? (all-network-interface-names)))
  266. (false-if-exception (delete-file #$pid-file))
  267. (let ((pid (fork+exec-command
  268. (cons* #$dhclient "-nw"
  269. "-pf" #$pid-file ifaces))))
  270. (and (zero? (cdr (waitpid pid)))
  271. (read-pid-file #$pid-file)))))
  272. (stop #~(make-kill-destructor))))
  273. isc-dhcp
  274. (description "Run @command{dhcp}, a Dynamic Host Configuration
  275. Protocol (DHCP) client, on all the non-loopback network interfaces.")))
  276. (define-record-type* <dhcpd-configuration>
  277. dhcpd-configuration make-dhcpd-configuration
  278. dhcpd-configuration?
  279. (package dhcpd-configuration-package ;<package>
  280. (default isc-dhcp))
  281. (config-file dhcpd-configuration-config-file ;file-like
  282. (default #f))
  283. (version dhcpd-configuration-version ;"4", "6", or "4o6"
  284. (default "4"))
  285. (run-directory dhcpd-configuration-run-directory
  286. (default "/run/dhcpd"))
  287. (lease-file dhcpd-configuration-lease-file
  288. (default "/var/db/dhcpd.leases"))
  289. (pid-file dhcpd-configuration-pid-file
  290. (default "/run/dhcpd/dhcpd.pid"))
  291. ;; list of strings, e.g. (list "enp0s25")
  292. (interfaces dhcpd-configuration-interfaces
  293. (default '())))
  294. (define dhcpd-shepherd-service
  295. (match-lambda
  296. (($ <dhcpd-configuration> package config-file version run-directory
  297. lease-file pid-file interfaces)
  298. (unless config-file
  299. (error "Must supply a config-file"))
  300. (list (shepherd-service
  301. ;; Allow users to easily run multiple versions simultaneously.
  302. (provision (list (string->symbol
  303. (string-append "dhcpv" version "-daemon"))))
  304. (documentation (string-append "Run the DHCPv" version " daemon"))
  305. (requirement '(networking))
  306. (start #~(make-forkexec-constructor
  307. '(#$(file-append package "/sbin/dhcpd")
  308. #$(string-append "-" version)
  309. "-lf" #$lease-file
  310. "-pf" #$pid-file
  311. "-cf" #$config-file
  312. #$@interfaces)
  313. #:pid-file #$pid-file))
  314. (stop #~(make-kill-destructor)))))))
  315. (define dhcpd-activation
  316. (match-lambda
  317. (($ <dhcpd-configuration> package config-file version run-directory
  318. lease-file pid-file interfaces)
  319. (with-imported-modules '((guix build utils))
  320. #~(begin
  321. (unless (file-exists? #$run-directory)
  322. (mkdir #$run-directory))
  323. ;; According to the DHCP manual (man dhcpd.leases), the lease
  324. ;; database must be present for dhcpd to start successfully.
  325. (unless (file-exists? #$lease-file)
  326. (with-output-to-file #$lease-file
  327. (lambda _ (display ""))))
  328. ;; Validate the config.
  329. (invoke/quiet
  330. #$(file-append package "/sbin/dhcpd")
  331. #$(string-append "-" version)
  332. "-t" "-cf" #$config-file))))))
  333. (define dhcpd-service-type
  334. (service-type
  335. (name 'dhcpd)
  336. (extensions
  337. (list (service-extension shepherd-root-service-type dhcpd-shepherd-service)
  338. (service-extension activation-service-type dhcpd-activation)))
  339. (description "Run a DHCP (Dynamic Host Configuration Protocol) daemon. The
  340. daemon is responsible for allocating IP addresses to its client.")))
  341. ;;;
  342. ;;; NTP.
  343. ;;;
  344. (define ntp-server-types (make-enumeration
  345. '(pool
  346. server
  347. peer
  348. broadcast
  349. manycastclient)))
  350. (define-record-type* <ntp-server>
  351. ntp-server make-ntp-server
  352. ntp-server?
  353. ;; The type can be one of the symbols of the NTP-SERVER-TYPE? enumeration.
  354. (type ntp-server-type
  355. (default 'server))
  356. (address ntp-server-address) ; a string
  357. ;; The list of options can contain single option names or tuples in the form
  358. ;; '(name value).
  359. (options ntp-server-options
  360. (default '())))
  361. (define (ntp-server->string ntp-server)
  362. ;; Serialize the NTP server object as a string, ready to use in the NTP
  363. ;; configuration file.
  364. (define (flatten lst)
  365. (reverse
  366. (let loop ((x lst)
  367. (res '()))
  368. (if (list? x)
  369. (fold loop res x)
  370. (cons (format #f "~a" x) res)))))
  371. (match ntp-server
  372. (($ <ntp-server> type address options)
  373. ;; XXX: It'd be neater if fields were validated at the syntax level (for
  374. ;; static ones at least). Perhaps the Guix record type could support a
  375. ;; predicate property on a field?
  376. (unless (enum-set-member? type ntp-server-types)
  377. (error "Invalid NTP server type" type))
  378. (string-join (cons* (symbol->string type)
  379. address
  380. (flatten options))))))
  381. (define %ntp-servers
  382. ;; Default set of NTP servers. These URLs are managed by the NTP Pool project.
  383. ;; Within Guix, Leo Famulari <leo@famulari.name> is the administrative contact
  384. ;; for this NTP pool "zone".
  385. (list
  386. (ntp-server
  387. (type 'pool)
  388. (address "0.guix.pool.ntp.org")
  389. (options '("iburst"))))) ;as recommended in the ntpd manual
  390. (define-record-type* <ntp-configuration>
  391. ntp-configuration make-ntp-configuration
  392. ntp-configuration?
  393. (ntp ntp-configuration-ntp
  394. (default ntp))
  395. (servers %ntp-configuration-servers ;list of <ntp-server> objects
  396. (default %ntp-servers))
  397. (allow-large-adjustment? ntp-allow-large-adjustment?
  398. (default #t))) ;as recommended in the ntpd manual
  399. (define (ntp-configuration-servers ntp-configuration)
  400. ;; A wrapper to support the deprecated form of this field.
  401. (let ((ntp-servers (%ntp-configuration-servers ntp-configuration)))
  402. (match ntp-servers
  403. (((? string?) (? string?) ...)
  404. (format (current-error-port) "warning: Defining NTP servers as strings is \
  405. deprecated. Please use <ntp-server> records instead.\n")
  406. (map (lambda (addr)
  407. (ntp-server
  408. (type 'server)
  409. (address addr)
  410. (options '()))) ntp-servers))
  411. ((($ <ntp-server>) ($ <ntp-server>) ...)
  412. ntp-servers))))
  413. (define ntp-shepherd-service
  414. (lambda (config)
  415. (match config
  416. (($ <ntp-configuration> ntp servers allow-large-adjustment?)
  417. (let ((servers (ntp-configuration-servers config)))
  418. ;; TODO: Add authentication support.
  419. (define config
  420. (string-append "driftfile /var/run/ntpd/ntp.drift\n"
  421. (string-join (map ntp-server->string servers)
  422. "\n")
  423. "
  424. # Disable status queries as a workaround for CVE-2013-5211:
  425. # <http://support.ntp.org/bin/view/Main/SecurityNotice#DRDoS_Amplification_Attack_using>.
  426. restrict default kod nomodify notrap nopeer noquery limited
  427. restrict -6 default kod nomodify notrap nopeer noquery limited
  428. # Yet, allow use of the local 'ntpq'.
  429. restrict 127.0.0.1
  430. restrict -6 ::1
  431. # This is required to use servers from a pool directive when using the 'nopeer'
  432. # option by default, as documented in the 'ntp.conf' manual.
  433. restrict source notrap nomodify noquery\n"))
  434. (define ntpd.conf
  435. (plain-file "ntpd.conf" config))
  436. (list (shepherd-service
  437. (provision '(ntpd))
  438. (documentation "Run the Network Time Protocol (NTP) daemon.")
  439. (requirement '(user-processes networking))
  440. (start #~(make-forkexec-constructor
  441. (list (string-append #$ntp "/bin/ntpd") "-n"
  442. "-c" #$ntpd.conf "-u" "ntpd"
  443. #$@(if allow-large-adjustment?
  444. '("-g")
  445. '()))
  446. #:log-file "/var/log/ntpd.log"))
  447. (stop #~(make-kill-destructor)))))))))
  448. (define %ntp-accounts
  449. (list (user-account
  450. (name "ntpd")
  451. (group "nogroup")
  452. (system? #t)
  453. (comment "NTP daemon user")
  454. (home-directory "/var/empty")
  455. (shell (file-append shadow "/sbin/nologin")))))
  456. (define (ntp-service-activation config)
  457. "Return the activation gexp for CONFIG."
  458. (with-imported-modules '((guix build utils))
  459. #~(begin
  460. (use-modules (guix build utils))
  461. (define %user
  462. (getpw "ntpd"))
  463. (let ((directory "/var/run/ntpd"))
  464. (mkdir-p directory)
  465. (chown directory (passwd:uid %user) (passwd:gid %user))))))
  466. (define ntp-service-type
  467. (service-type (name 'ntp)
  468. (extensions
  469. (list (service-extension shepherd-root-service-type
  470. ntp-shepherd-service)
  471. (service-extension account-service-type
  472. (const %ntp-accounts))
  473. (service-extension activation-service-type
  474. ntp-service-activation)))
  475. (description
  476. "Run the @command{ntpd}, the Network Time Protocol (NTP)
  477. daemon of the @uref{http://www.ntp.org, Network Time Foundation}. The daemon
  478. will keep the system clock synchronized with that of the given servers.")
  479. (default-value (ntp-configuration))))
  480. ;;;
  481. ;;; OpenNTPD.
  482. ;;;
  483. (define %openntpd-servers
  484. (map ntp-server-address %ntp-servers))
  485. (define-record-type* <openntpd-configuration>
  486. openntpd-configuration make-openntpd-configuration
  487. openntpd-configuration?
  488. (openntpd openntpd-configuration-openntpd
  489. (default openntpd))
  490. (listen-on openntpd-listen-on
  491. (default '("127.0.0.1"
  492. "::1")))
  493. (query-from openntpd-query-from
  494. (default '()))
  495. (sensor openntpd-sensor
  496. (default '()))
  497. (server openntpd-server
  498. (default '()))
  499. (servers openntpd-servers
  500. (default %openntpd-servers))
  501. (constraint-from openntpd-constraint-from
  502. (default '()))
  503. (constraints-from openntpd-constraints-from
  504. (default '())))
  505. (define (openntpd-configuration->string config)
  506. (define (quote-field? name)
  507. (member name '("constraints from")))
  508. (match-record config <openntpd-configuration>
  509. (listen-on query-from sensor server servers constraint-from
  510. constraints-from)
  511. (string-append
  512. (string-join
  513. (concatenate
  514. (filter-map (lambda (field values)
  515. (match values
  516. (() #f) ;discard entry with filter-map
  517. ((val ...) ;validate value type
  518. (map (lambda (value)
  519. (if (quote-field? field)
  520. (format #f "~a \"~a\"" field value)
  521. (format #f "~a ~a" field value)))
  522. values))))
  523. ;; The entry names.
  524. '("listen on" "query from" "sensor" "server" "servers"
  525. "constraint from" "constraints from")
  526. ;; The corresponding entry values.
  527. (list listen-on query-from sensor server servers
  528. constraint-from constraints-from)))
  529. "\n")
  530. "\n"))) ;add a trailing newline
  531. (define (openntpd-shepherd-service config)
  532. (let ((openntpd (openntpd-configuration-openntpd config)))
  533. (define ntpd.conf
  534. (plain-file "ntpd.conf" (openntpd-configuration->string config)))
  535. (list (shepherd-service
  536. (provision '(ntpd))
  537. (documentation "Run the Network Time Protocol (NTP) daemon.")
  538. (requirement '(user-processes networking))
  539. (start #~(make-forkexec-constructor
  540. (list (string-append #$openntpd "/sbin/ntpd")
  541. "-f" #$ntpd.conf
  542. "-d") ;; don't daemonize
  543. ;; When ntpd is daemonized it repeatedly tries to respawn
  544. ;; while running, leading shepherd to disable it. To
  545. ;; prevent spamming stderr, redirect output to logfile.
  546. #:log-file "/var/log/ntpd"))
  547. (stop #~(make-kill-destructor))))))
  548. (define (openntpd-service-activation config)
  549. "Return the activation gexp for CONFIG."
  550. (with-imported-modules '((guix build utils))
  551. #~(begin
  552. (use-modules (guix build utils))
  553. (mkdir-p "/var/db")
  554. (mkdir-p "/var/run")
  555. (unless (file-exists? "/var/db/ntpd.drift")
  556. (with-output-to-file "/var/db/ntpd.drift"
  557. (lambda _
  558. (format #t "0.0")))))))
  559. (define openntpd-service-type
  560. (service-type (name 'openntpd)
  561. (extensions
  562. (list (service-extension shepherd-root-service-type
  563. openntpd-shepherd-service)
  564. (service-extension account-service-type
  565. (const %ntp-accounts))
  566. (service-extension profile-service-type
  567. (compose list openntpd-configuration-openntpd))
  568. (service-extension activation-service-type
  569. openntpd-service-activation)))
  570. (default-value (openntpd-configuration))
  571. (description
  572. "Run the @command{ntpd}, the Network Time Protocol (NTP)
  573. daemon, as implemented by @uref{http://www.openntpd.org, OpenNTPD}. The
  574. daemon will keep the system clock synchronized with that of the given servers.")))
  575. ;;;
  576. ;;; Inetd.
  577. ;;;
  578. (define-record-type* <inetd-configuration> inetd-configuration
  579. make-inetd-configuration
  580. inetd-configuration?
  581. (program inetd-configuration-program ;file-like
  582. (default (file-append inetutils "/libexec/inetd")))
  583. (entries inetd-configuration-entries ;list of <inetd-entry>
  584. (default '())))
  585. (define-record-type* <inetd-entry> inetd-entry make-inetd-entry
  586. inetd-entry?
  587. (node inetd-entry-node ;string or #f
  588. (default #f))
  589. (name inetd-entry-name) ;string, from /etc/services
  590. (socket-type inetd-entry-socket-type) ;stream | dgram | raw |
  591. ;rdm | seqpacket
  592. (protocol inetd-entry-protocol) ;string, from /etc/protocols
  593. (wait? inetd-entry-wait? ;Boolean
  594. (default #t))
  595. (user inetd-entry-user) ;string
  596. (program inetd-entry-program ;string or file-like object
  597. (default "internal"))
  598. (arguments inetd-entry-arguments ;list of strings or file-like objects
  599. (default '())))
  600. (define (inetd-config-file entries)
  601. (apply mixed-text-file "inetd.conf"
  602. (map
  603. (lambda (entry)
  604. (let* ((node (inetd-entry-node entry))
  605. (name (inetd-entry-name entry))
  606. (socket
  607. (if node (string-append node ":" name) name))
  608. (type
  609. (match (inetd-entry-socket-type entry)
  610. ((or 'stream 'dgram 'raw 'rdm 'seqpacket)
  611. (symbol->string (inetd-entry-socket-type entry)))))
  612. (protocol (inetd-entry-protocol entry))
  613. (wait (if (inetd-entry-wait? entry) "wait" "nowait"))
  614. (user (inetd-entry-user entry))
  615. (program (inetd-entry-program entry))
  616. (args (inetd-entry-arguments entry)))
  617. #~(string-append
  618. (string-join
  619. (list #$@(list socket type protocol wait user program) #$@args)
  620. " ") "\n")))
  621. entries)))
  622. (define inetd-shepherd-service
  623. (match-lambda
  624. (($ <inetd-configuration> program ()) '()) ; empty list of entries -> do nothing
  625. (($ <inetd-configuration> program entries)
  626. (list
  627. (shepherd-service
  628. (documentation "Run inetd.")
  629. (provision '(inetd))
  630. (requirement '(user-processes networking syslogd))
  631. (start #~(make-forkexec-constructor
  632. (list #$program #$(inetd-config-file entries))
  633. #:pid-file "/var/run/inetd.pid"))
  634. (stop #~(make-kill-destructor)))))))
  635. (define-public inetd-service-type
  636. (service-type
  637. (name 'inetd)
  638. (extensions
  639. (list (service-extension shepherd-root-service-type
  640. inetd-shepherd-service)))
  641. ;; The service can be extended with additional lists of entries.
  642. (compose concatenate)
  643. (extend (lambda (config entries)
  644. (inetd-configuration
  645. (inherit config)
  646. (entries (append (inetd-configuration-entries config)
  647. entries)))))
  648. (description
  649. "Start @command{inetd}, the @dfn{Internet superserver}. It is responsible
  650. for listening on Internet sockets and spawning the corresponding services on
  651. demand.")))
  652. ;;;
  653. ;;; OpenDHT, the distributed hash table network used by Jami
  654. ;;;
  655. (define-maybe/no-serialization number)
  656. (define-maybe/no-serialization string)
  657. ;;; To generate the documentation of the following configuration record, you
  658. ;;; can evaluate: (configuration->documentation 'opendht-configuration)
  659. (define-configuration/no-serialization opendht-configuration
  660. (opendht
  661. (package opendht)
  662. "The @code{opendht} package to use.")
  663. (peer-discovery?
  664. (boolean #false)
  665. "Whether to enable the multicast local peer discovery mechanism.")
  666. (enable-logging?
  667. (boolean #false)
  668. "Whether to enable logging messages to syslog. It is disabled by default
  669. as it is rather verbose.")
  670. (debug?
  671. (boolean #false)
  672. "Whether to enable debug-level logging messages. This has no effect if
  673. logging is disabled.")
  674. (bootstrap-host
  675. (maybe-string "bootstrap.jami.net:4222")
  676. "The node host name that is used to make the first connection to the
  677. network. A specific port value can be provided by appending the @code{:PORT}
  678. suffix. By default, it uses the Jami bootstrap nodes, but any host can be
  679. specified here. It's also possible to disable bootstrapping by setting this
  680. to the @code{'disabled} symbol.")
  681. (port
  682. (maybe-number 4222)
  683. "The UDP port to bind to. When set to @code{'disabled}, an available port
  684. is automatically selected.")
  685. (proxy-server-port
  686. (maybe-number 'disabled)
  687. "Spawn a proxy server listening on the specified port.")
  688. (proxy-server-port-tls
  689. (maybe-number 'disabled)
  690. "Spawn a proxy server listening to TLS connections on the specified
  691. port."))
  692. (define %opendht-accounts
  693. ;; User account and groups for Tor.
  694. (list (user-group (name "opendht") (system? #t))
  695. (user-account
  696. (name "opendht")
  697. (group "opendht")
  698. (system? #t)
  699. (comment "OpenDHT daemon user")
  700. (home-directory "/var/empty")
  701. (shell (file-append shadow "/sbin/nologin")))))
  702. (define (opendht-configuration->command-line-arguments config)
  703. "Derive the command line arguments used to launch the OpenDHT daemon from
  704. CONFIG, an <opendht-configuration> object."
  705. (match-record config <opendht-configuration>
  706. (opendht bootstrap-host enable-logging? port debug? peer-discovery?
  707. proxy-server-port proxy-server-port-tls)
  708. (let ((dhtnode #~(string-append #$opendht:tools "/bin/dhtnode")))
  709. `(,dhtnode
  710. "--service" ;non-forking mode
  711. ,@(if (string? bootstrap-host)
  712. (list "--bootstrap" bootstrap-host))
  713. ,@(if enable-logging?
  714. (list "--syslog")
  715. '())
  716. ,@(if (number? port)
  717. (list "--port" (number->string port))
  718. '())
  719. ,@(if debug?
  720. (list "--verbose")
  721. '())
  722. ,@(if peer-discovery?
  723. (list "--peer-discovery")
  724. '())
  725. ,@(if (number? proxy-server-port)
  726. (list "--proxyserver" (number->string proxy-server-port))
  727. '())
  728. ,@(if (number? proxy-server-port-tls)
  729. (list "--proxyserverssl" (number->string proxy-server-port-tls))
  730. '())))))
  731. (define (opendht-shepherd-service config)
  732. "Return a <shepherd-service> running OpenDHT."
  733. (with-imported-modules (source-module-closure
  734. '((gnu build shepherd)
  735. (gnu system file-systems)))
  736. (shepherd-service
  737. (documentation "Run an OpenDHT node.")
  738. (provision '(opendht dhtnode dhtproxy))
  739. (requirement '(networking syslogd))
  740. (modules '((gnu build shepherd)
  741. (gnu system file-systems)))
  742. (start #~(make-forkexec-constructor/container
  743. (list #$@(opendht-configuration->command-line-arguments config))
  744. #:mappings (list (file-system-mapping
  745. (source "/dev/log") ;for syslog
  746. (target source)))
  747. #:user "opendht"
  748. #:group "opendht"))
  749. (stop #~(make-kill-destructor)))))
  750. (define opendht-service-type
  751. (service-type
  752. (name 'opendht)
  753. (default-value (opendht-configuration))
  754. (extensions
  755. (list (service-extension shepherd-root-service-type
  756. (compose list opendht-shepherd-service))
  757. (service-extension account-service-type
  758. (const %opendht-accounts))))
  759. (description "Run the OpenDHT @command{dhtnode} command that allows
  760. participating in the distributed hash table based OpenDHT network. The
  761. service can be configured to act as a proxy to the distributed network, which
  762. can be useful for portable devices where minimizing energy consumption is
  763. paramount. OpenDHT was originally based on Kademlia and adapted for
  764. applications in communication. It is used by Jami, for example.")))
  765. ;;;
  766. ;;; Tor.
  767. ;;;
  768. (define-record-type* <tor-configuration>
  769. tor-configuration make-tor-configuration
  770. tor-configuration?
  771. (tor tor-configuration-tor
  772. (default tor))
  773. (config-file tor-configuration-config-file
  774. (default (plain-file "empty" "")))
  775. (hidden-services tor-configuration-hidden-services
  776. (default '()))
  777. (socks-socket-type tor-configuration-socks-socket-type ; 'tcp or 'unix
  778. (default 'tcp))
  779. (control-socket? tor-control-socket-path
  780. (default #f)))
  781. (define %tor-accounts
  782. ;; User account and groups for Tor.
  783. (list (user-group (name "tor") (system? #t))
  784. (user-account
  785. (name "tor")
  786. (group "tor")
  787. (system? #t)
  788. (comment "Tor daemon user")
  789. (home-directory "/var/empty")
  790. (shell (file-append shadow "/sbin/nologin")))))
  791. (define-record-type <hidden-service>
  792. (hidden-service name mapping)
  793. hidden-service?
  794. (name hidden-service-name) ;string
  795. (mapping hidden-service-mapping)) ;list of port/address tuples
  796. (define (tor-configuration->torrc config)
  797. "Return a 'torrc' file for CONFIG."
  798. (match config
  799. (($ <tor-configuration> tor config-file services
  800. socks-socket-type control-socket?)
  801. (computed-file
  802. "torrc"
  803. (with-imported-modules '((guix build utils))
  804. #~(begin
  805. (use-modules (guix build utils)
  806. (ice-9 match))
  807. (call-with-output-file #$output
  808. (lambda (port)
  809. (display "\
  810. ### These lines were generated from your system configuration:
  811. User tor
  812. DataDirectory /var/lib/tor
  813. PidFile /var/run/tor/tor.pid
  814. Log notice syslog\n" port)
  815. (when (eq? 'unix '#$socks-socket-type)
  816. (display "\
  817. SocksPort unix:/var/run/tor/socks-sock
  818. UnixSocksGroupWritable 1\n" port))
  819. (when #$control-socket?
  820. (display "\
  821. ControlSocket unix:/var/run/tor/control-sock GroupWritable RelaxDirModeCheck
  822. ControlSocketsGroupWritable 1\n"
  823. port))
  824. (for-each (match-lambda
  825. ((service (ports hosts) ...)
  826. (format port "\
  827. HiddenServiceDir /var/lib/tor/hidden-services/~a~%"
  828. service)
  829. (for-each (lambda (tcp-port host)
  830. (format port "\
  831. HiddenServicePort ~a ~a~%"
  832. tcp-port host))
  833. ports hosts)))
  834. '#$(map (match-lambda
  835. (($ <hidden-service> name mapping)
  836. (cons name mapping)))
  837. services))
  838. (display "\
  839. ### End of automatically generated lines.\n\n" port)
  840. ;; Append the user's config file.
  841. (call-with-input-file #$config-file
  842. (lambda (input)
  843. (dump-port input port)))
  844. #t))))))))
  845. (define (tor-shepherd-service config)
  846. "Return a <shepherd-service> running Tor."
  847. (match config
  848. (($ <tor-configuration> tor)
  849. (let ((torrc (tor-configuration->torrc config)))
  850. (with-imported-modules (source-module-closure
  851. '((gnu build shepherd)
  852. (gnu system file-systems)))
  853. (list (shepherd-service
  854. (provision '(tor))
  855. ;; Tor needs at least one network interface to be up, hence the
  856. ;; dependency on 'loopback'.
  857. (requirement '(user-processes loopback syslogd))
  858. (modules '((gnu build shepherd)
  859. (gnu system file-systems)))
  860. (start #~(make-forkexec-constructor/container
  861. (list #$(file-append tor "/bin/tor") "-f" #$torrc)
  862. #:log-file "/var/log/tor.log"
  863. #:mappings (list (file-system-mapping
  864. (source "/var/lib/tor")
  865. (target source)
  866. (writable? #t))
  867. (file-system-mapping
  868. (source "/dev/log") ;for syslog
  869. (target source))
  870. (file-system-mapping
  871. (source "/var/run/tor")
  872. (target source)
  873. (writable? #t)))
  874. #:pid-file "/var/run/tor/tor.pid"))
  875. (stop #~(make-kill-destructor))
  876. (documentation "Run the Tor anonymous network overlay."))))))))
  877. (define (tor-activation config)
  878. "Set up directories for Tor and its hidden services, if any."
  879. #~(begin
  880. (use-modules (guix build utils))
  881. (define %user
  882. (getpw "tor"))
  883. (define (initialize service)
  884. (let ((directory (string-append "/var/lib/tor/hidden-services/"
  885. service)))
  886. (mkdir-p directory)
  887. (chown directory (passwd:uid %user) (passwd:gid %user))
  888. ;; The daemon bails out if we give wider permissions.
  889. (chmod directory #o700)))
  890. ;; Allow Tor to write its PID file.
  891. (mkdir-p "/var/run/tor")
  892. (chown "/var/run/tor" (passwd:uid %user) (passwd:gid %user))
  893. ;; Set the group permissions to rw so that if the system administrator
  894. ;; has specified UnixSocksGroupWritable=1 in their torrc file, members
  895. ;; of the "tor" group will be able to use the SOCKS socket.
  896. (chmod "/var/run/tor" #o750)
  897. ;; Allow Tor to access the hidden services' directories.
  898. (mkdir-p "/var/lib/tor")
  899. (chown "/var/lib/tor" (passwd:uid %user) (passwd:gid %user))
  900. (chmod "/var/lib/tor" #o700)
  901. ;; Make sure /var/lib is accessible to the 'tor' user.
  902. (chmod "/var/lib" #o755)
  903. (for-each initialize
  904. '#$(map hidden-service-name
  905. (tor-configuration-hidden-services config)))))
  906. (define tor-service-type
  907. (service-type (name 'tor)
  908. (extensions
  909. (list (service-extension shepherd-root-service-type
  910. tor-shepherd-service)
  911. (service-extension account-service-type
  912. (const %tor-accounts))
  913. (service-extension activation-service-type
  914. tor-activation)))
  915. ;; This can be extended with hidden services.
  916. (compose concatenate)
  917. (extend (lambda (config services)
  918. (tor-configuration
  919. (inherit config)
  920. (hidden-services
  921. (append (tor-configuration-hidden-services config)
  922. services)))))
  923. (default-value (tor-configuration))
  924. (description
  925. "Run the @uref{https://torproject.org, Tor} anonymous
  926. networking daemon.")))
  927. (define tor-hidden-service-type
  928. ;; A type that extends Tor with hidden services.
  929. (service-type (name 'tor-hidden-service)
  930. (extensions
  931. (list (service-extension tor-service-type list)))
  932. (description
  933. "Define a new Tor @dfn{hidden service}.")))
  934. (define (tor-hidden-service name mapping)
  935. "Define a new Tor @dfn{hidden service} called @var{name} and implementing
  936. @var{mapping}. @var{mapping} is a list of port/host tuples, such as:
  937. @example
  938. '((22 \"127.0.0.1:22\")
  939. (80 \"127.0.0.1:8080\"))
  940. @end example
  941. In this example, port 22 of the hidden service is mapped to local port 22, and
  942. port 80 is mapped to local port 8080.
  943. This creates a @file{/var/lib/tor/hidden-services/@var{name}} directory, where
  944. the @file{hostname} file contains the @code{.onion} host name for the hidden
  945. service.
  946. See @uref{https://www.torproject.org/docs/tor-hidden-service.html.en, the Tor
  947. project's documentation} for more information."
  948. (service tor-hidden-service-type
  949. (hidden-service name mapping)))
  950. ;;;
  951. ;;; Wicd.
  952. ;;;
  953. (define %wicd-activation
  954. ;; Activation gexp for Wicd.
  955. #~(begin
  956. (use-modules (guix build utils))
  957. (mkdir-p "/etc/wicd")
  958. (let ((file-name "/etc/wicd/dhclient.conf.template.default"))
  959. (unless (file-exists? file-name)
  960. (copy-file (string-append #$wicd file-name)
  961. file-name)))
  962. ;; Wicd invokes 'wpa_supplicant', which needs this directory for its
  963. ;; named socket files.
  964. (mkdir-p "/var/run/wpa_supplicant")
  965. (chmod "/var/run/wpa_supplicant" #o750)))
  966. (define (wicd-shepherd-service wicd)
  967. "Return a shepherd service for WICD."
  968. (list (shepherd-service
  969. (documentation "Run the Wicd network manager.")
  970. (provision '(networking))
  971. (requirement '(user-processes dbus-system loopback))
  972. (start #~(make-forkexec-constructor
  973. (list (string-append #$wicd "/sbin/wicd")
  974. "--no-daemon")))
  975. (stop #~(make-kill-destructor)))))
  976. (define wicd-service-type
  977. (service-type (name 'wicd)
  978. (extensions
  979. (list (service-extension shepherd-root-service-type
  980. wicd-shepherd-service)
  981. (service-extension dbus-root-service-type
  982. list)
  983. (service-extension activation-service-type
  984. (const %wicd-activation))
  985. ;; Add Wicd to the global profile.
  986. (service-extension profile-service-type list)))
  987. (description
  988. "Run @url{https://launchpad.net/wicd,Wicd}, a network
  989. management daemon that aims to simplify wired and wireless networking.")))
  990. (define* (wicd-service #:key (wicd wicd))
  991. "Return a service that runs @url{https://launchpad.net/wicd,Wicd}, a network
  992. management daemon that aims to simplify wired and wireless networking.
  993. This service adds the @var{wicd} package to the global profile, providing
  994. several commands to interact with the daemon and configure networking:
  995. @command{wicd-client}, a graphical user interface, and the @command{wicd-cli}
  996. and @command{wicd-curses} user interfaces."
  997. (service wicd-service-type wicd))
  998. ;;;
  999. ;;; ModemManager
  1000. ;;;
  1001. (define-record-type* <modem-manager-configuration>
  1002. modem-manager-configuration make-modem-manager-configuration
  1003. modem-manager-configuration?
  1004. (modem-manager modem-manager-configuration-modem-manager
  1005. (default modem-manager)))
  1006. ;;;
  1007. ;;; NetworkManager
  1008. ;;;
  1009. (define-record-type* <network-manager-configuration>
  1010. network-manager-configuration make-network-manager-configuration
  1011. network-manager-configuration?
  1012. (network-manager network-manager-configuration-network-manager
  1013. (default network-manager))
  1014. (dns network-manager-configuration-dns
  1015. (default "default"))
  1016. (vpn-plugins network-manager-configuration-vpn-plugins ;list of <package>
  1017. (default '())))
  1018. (define network-manager-activation
  1019. ;; Activation gexp for NetworkManager
  1020. (match-lambda
  1021. (($ <network-manager-configuration> network-manager dns vpn-plugins)
  1022. #~(begin
  1023. (use-modules (guix build utils))
  1024. (mkdir-p "/etc/NetworkManager/system-connections")
  1025. #$@(if (equal? dns "dnsmasq")
  1026. ;; create directory to store dnsmasq lease file
  1027. '((mkdir-p "/var/lib/misc"))
  1028. '())))))
  1029. (define (vpn-plugin-directory plugins)
  1030. "Return a directory containing PLUGINS, the NM VPN plugins."
  1031. (directory-union "network-manager-vpn-plugins" plugins))
  1032. (define (network-manager-accounts config)
  1033. "Return the list of <user-account> and <user-group> for CONFIG."
  1034. (define nologin
  1035. (file-append shadow "/sbin/nologin"))
  1036. (define accounts
  1037. (append-map (lambda (package)
  1038. (map (lambda (name)
  1039. (user-account (system? #t)
  1040. (name name)
  1041. (group "network-manager")
  1042. (comment "NetworkManager helper")
  1043. (home-directory "/var/empty")
  1044. (create-home-directory? #f)
  1045. (shell nologin)))
  1046. (or (assoc-ref (package-properties package)
  1047. 'user-accounts)
  1048. '())))
  1049. (network-manager-configuration-vpn-plugins config)))
  1050. (match accounts
  1051. (()
  1052. '())
  1053. (_
  1054. (cons (user-group (name "network-manager") (system? #t))
  1055. accounts))))
  1056. (define network-manager-environment
  1057. (match-lambda
  1058. (($ <network-manager-configuration> network-manager dns vpn-plugins)
  1059. ;; Define this variable in the global environment such that
  1060. ;; "nmcli connection import type openvpn file foo.ovpn" works.
  1061. `(("NM_VPN_PLUGIN_DIR"
  1062. . ,(file-append (vpn-plugin-directory vpn-plugins)
  1063. "/lib/NetworkManager/VPN"))))))
  1064. (define network-manager-shepherd-service
  1065. (match-lambda
  1066. (($ <network-manager-configuration> network-manager dns vpn-plugins)
  1067. (let ((conf (plain-file "NetworkManager.conf"
  1068. (string-append "[main]\ndns=" dns "\n")))
  1069. (vpn (vpn-plugin-directory vpn-plugins)))
  1070. (list (shepherd-service
  1071. (documentation "Run the NetworkManager.")
  1072. (provision '(networking))
  1073. (requirement '(user-processes dbus-system wpa-supplicant loopback))
  1074. (start #~(make-forkexec-constructor
  1075. (list (string-append #$network-manager
  1076. "/sbin/NetworkManager")
  1077. (string-append "--config=" #$conf)
  1078. "--no-daemon")
  1079. #:environment-variables
  1080. (list (string-append "NM_VPN_PLUGIN_DIR=" #$vpn
  1081. "/lib/NetworkManager/VPN")
  1082. ;; Override non-existent default users
  1083. "NM_OPENVPN_USER="
  1084. "NM_OPENVPN_GROUP=")))
  1085. (stop #~(make-kill-destructor))))))))
  1086. (define network-manager-service-type
  1087. (let
  1088. ((config->packages
  1089. (match-lambda
  1090. (($ <network-manager-configuration> network-manager _ vpn-plugins)
  1091. `(,network-manager ,@vpn-plugins)))))
  1092. (service-type
  1093. (name 'network-manager)
  1094. (extensions
  1095. (list (service-extension shepherd-root-service-type
  1096. network-manager-shepherd-service)
  1097. (service-extension dbus-root-service-type config->packages)
  1098. (service-extension polkit-service-type
  1099. (compose
  1100. list
  1101. network-manager-configuration-network-manager))
  1102. (service-extension account-service-type
  1103. network-manager-accounts)
  1104. (service-extension activation-service-type
  1105. network-manager-activation)
  1106. (service-extension session-environment-service-type
  1107. network-manager-environment)
  1108. ;; Add network-manager to the system profile.
  1109. (service-extension profile-service-type config->packages)))
  1110. (default-value (network-manager-configuration))
  1111. (description
  1112. "Run @uref{https://wiki.gnome.org/Projects/NetworkManager,
  1113. NetworkManager}, a network management daemon that aims to simplify wired and
  1114. wireless networking."))))
  1115. ;;;
  1116. ;;; Connman
  1117. ;;;
  1118. (define-record-type* <connman-configuration>
  1119. connman-configuration make-connman-configuration
  1120. connman-configuration?
  1121. (connman connman-configuration-connman
  1122. (default connman))
  1123. (disable-vpn? connman-configuration-disable-vpn?
  1124. (default #f)))
  1125. (define (connman-activation config)
  1126. (let ((disable-vpn? (connman-configuration-disable-vpn? config)))
  1127. (with-imported-modules '((guix build utils))
  1128. #~(begin
  1129. (use-modules (guix build utils))
  1130. (mkdir-p "/var/lib/connman/")
  1131. (unless #$disable-vpn?
  1132. (mkdir-p "/var/lib/connman-vpn/"))))))
  1133. (define (connman-shepherd-service config)
  1134. "Return a shepherd service for Connman"
  1135. (and
  1136. (connman-configuration? config)
  1137. (let ((connman (connman-configuration-connman config))
  1138. (disable-vpn? (connman-configuration-disable-vpn? config)))
  1139. (list (shepherd-service
  1140. (documentation "Run Connman")
  1141. (provision '(networking))
  1142. (requirement
  1143. '(user-processes dbus-system loopback wpa-supplicant))
  1144. (start #~(make-forkexec-constructor
  1145. (list (string-append #$connman
  1146. "/sbin/connmand")
  1147. "--nodaemon"
  1148. "--nodnsproxy"
  1149. #$@(if disable-vpn? '("--noplugin=vpn") '()))
  1150. ;; As connman(8) notes, when passing '-n', connman
  1151. ;; "directs log output to the controlling terminal in
  1152. ;; addition to syslog." Redirect stdout and stderr
  1153. ;; to avoid spamming the console (XXX: for some reason
  1154. ;; redirecting to /dev/null doesn't work.)
  1155. #:log-file "/var/log/connman.log"))
  1156. (stop #~(make-kill-destructor)))))))
  1157. (define connman-service-type
  1158. (let ((connman-package (compose list connman-configuration-connman)))
  1159. (service-type (name 'connman)
  1160. (extensions
  1161. (list (service-extension shepherd-root-service-type
  1162. connman-shepherd-service)
  1163. (service-extension polkit-service-type
  1164. connman-package)
  1165. (service-extension dbus-root-service-type
  1166. connman-package)
  1167. (service-extension activation-service-type
  1168. connman-activation)
  1169. ;; Add connman to the system profile.
  1170. (service-extension profile-service-type
  1171. connman-package)))
  1172. (default-value (connman-configuration))
  1173. (description
  1174. "Run @url{https://01.org/connman,Connman},
  1175. a network connection manager."))))
  1176. ;;;
  1177. ;;; Modem manager
  1178. ;;;
  1179. (define modem-manager-service-type
  1180. (let ((config->package
  1181. (match-lambda
  1182. (($ <modem-manager-configuration> modem-manager)
  1183. (list modem-manager)))))
  1184. (service-type (name 'modem-manager)
  1185. (extensions
  1186. (list (service-extension dbus-root-service-type
  1187. config->package)
  1188. (service-extension udev-service-type
  1189. config->package)
  1190. (service-extension polkit-service-type
  1191. config->package)))
  1192. (default-value (modem-manager-configuration))
  1193. (description
  1194. "Run @uref{https://wiki.gnome.org/Projects/ModemManager,
  1195. ModemManager}, a modem management daemon that aims to simplify dialup
  1196. networking."))))
  1197. ;;;
  1198. ;;; USB_ModeSwitch
  1199. ;;;
  1200. (define-record-type* <usb-modeswitch-configuration>
  1201. usb-modeswitch-configuration make-usb-modeswitch-configuration
  1202. usb-modeswitch-configuration?
  1203. (usb-modeswitch usb-modeswitch-configuration-usb-modeswitch
  1204. (default usb-modeswitch))
  1205. (usb-modeswitch-data usb-modeswitch-configuration-usb-modeswitch-data
  1206. (default usb-modeswitch-data))
  1207. (config-file usb-modeswitch-configuration-config-file
  1208. (default #~(string-append #$usb-modeswitch:dispatcher
  1209. "/etc/usb_modeswitch.conf"))))
  1210. (define (usb-modeswitch-sh usb-modeswitch config-file)
  1211. "Build a copy of usb_modeswitch.sh located in package USB-MODESWITCH,
  1212. modified to pass the CONFIG-FILE in its calls to usb_modeswitch_dispatcher,
  1213. and wrap it to actually find the dispatcher in USB-MODESWITCH. The script
  1214. will be run by USB_ModeSwitch’s udev rules file when a modeswitchable USB
  1215. device is detected."
  1216. (computed-file
  1217. "usb_modeswitch-sh"
  1218. (with-imported-modules '((guix build utils))
  1219. #~(begin
  1220. (use-modules (guix build utils))
  1221. (let ((cfg-param
  1222. #$(if config-file
  1223. #~(string-append " --config-file=" #$config-file)
  1224. "")))
  1225. (mkdir #$output)
  1226. (install-file (string-append #$usb-modeswitch:dispatcher
  1227. "/lib/udev/usb_modeswitch")
  1228. #$output)
  1229. ;; insert CFG-PARAM into usb_modeswitch_dispatcher command-lines
  1230. (substitute* (string-append #$output "/usb_modeswitch")
  1231. (("(exec usb_modeswitch_dispatcher .*)( 2>>)" _ left right)
  1232. (string-append left cfg-param right))
  1233. (("(exec usb_modeswitch_dispatcher .*)( &)" _ left right)
  1234. (string-append left cfg-param right)))
  1235. ;; wrap-program needs bash in PATH:
  1236. (putenv (string-append "PATH=" #$bash "/bin"))
  1237. (wrap-program (string-append #$output "/usb_modeswitch")
  1238. `("PATH" ":" = (,(string-append #$coreutils "/bin")
  1239. ,(string-append
  1240. #$usb-modeswitch:dispatcher
  1241. "/bin")))))))))
  1242. (define (usb-modeswitch-configuration->udev-rules config)
  1243. "Build a rules file for extending udev-service-type from the rules in the
  1244. usb-modeswitch package specified in CONFIG. The rules file will invoke
  1245. usb_modeswitch.sh from the usb-modeswitch package, modified to pass the right
  1246. config file."
  1247. (match config
  1248. (($ <usb-modeswitch-configuration> usb-modeswitch data config-file)
  1249. (computed-file
  1250. "usb_modeswitch.rules"
  1251. (with-imported-modules '((guix build utils))
  1252. #~(begin
  1253. (use-modules (guix build utils))
  1254. (let ((in (string-append #$data "/udev/40-usb_modeswitch.rules"))
  1255. (out (string-append #$output "/lib/udev/rules.d"))
  1256. (script #$(usb-modeswitch-sh usb-modeswitch config-file)))
  1257. (mkdir-p out)
  1258. (chdir out)
  1259. (install-file in out)
  1260. (substitute* "40-usb_modeswitch.rules"
  1261. (("PROGRAM=\"usb_modeswitch")
  1262. (string-append "PROGRAM=\"" script "/usb_modeswitch"))
  1263. (("RUN\\+=\"usb_modeswitch")
  1264. (string-append "RUN+=\"" script "/usb_modeswitch"))))))))))
  1265. (define usb-modeswitch-service-type
  1266. (service-type
  1267. (name 'usb-modeswitch)
  1268. (extensions
  1269. (list
  1270. (service-extension
  1271. udev-service-type
  1272. (lambda (config)
  1273. (let ((rules (usb-modeswitch-configuration->udev-rules config)))
  1274. (list rules))))))
  1275. (default-value (usb-modeswitch-configuration))
  1276. (description "Run @uref{http://www.draisberghof.de/usb_modeswitch/,
  1277. USB_ModeSwitch}, a mode switching tool for controlling USB devices with
  1278. multiple @dfn{modes}. When plugged in for the first time many USB
  1279. devices (primarily high-speed WAN modems) act like a flash storage containing
  1280. installers for Windows drivers. USB_ModeSwitch replays the sequence the
  1281. Windows drivers would send to switch their mode from storage to modem (or
  1282. whatever the thing is supposed to do).")))
  1283. ;;;
  1284. ;;; WPA supplicant
  1285. ;;;
  1286. (define-record-type* <wpa-supplicant-configuration>
  1287. wpa-supplicant-configuration make-wpa-supplicant-configuration
  1288. wpa-supplicant-configuration?
  1289. (wpa-supplicant wpa-supplicant-configuration-wpa-supplicant ;<package>
  1290. (default wpa-supplicant))
  1291. (requirement wpa-supplicant-configuration-requirement ;list of symbols
  1292. (default '(user-processes loopback syslogd)))
  1293. (pid-file wpa-supplicant-configuration-pid-file ;string
  1294. (default "/var/run/wpa_supplicant.pid"))
  1295. (dbus? wpa-supplicant-configuration-dbus? ;Boolean
  1296. (default #t))
  1297. (interface wpa-supplicant-configuration-interface ;#f | string
  1298. (default #f))
  1299. (config-file wpa-supplicant-configuration-config-file ;#f | <file-like>
  1300. (default #f))
  1301. (extra-options wpa-supplicant-configuration-extra-options ;list of strings
  1302. (default '())))
  1303. (define wpa-supplicant-shepherd-service
  1304. (match-lambda
  1305. (($ <wpa-supplicant-configuration> wpa-supplicant requirement pid-file dbus?
  1306. interface config-file extra-options)
  1307. (list (shepherd-service
  1308. (documentation "Run the WPA supplicant daemon")
  1309. (provision '(wpa-supplicant))
  1310. (requirement (if dbus?
  1311. (cons 'dbus-system requirement)
  1312. requirement))
  1313. (start #~(make-forkexec-constructor
  1314. (list (string-append #$wpa-supplicant
  1315. "/sbin/wpa_supplicant")
  1316. (string-append "-P" #$pid-file)
  1317. "-B" ;run in background
  1318. "-s" ;log to syslogd
  1319. #$@(if dbus?
  1320. #~("-u")
  1321. #~())
  1322. #$@(if interface
  1323. #~((string-append "-i" #$interface))
  1324. #~())
  1325. #$@(if config-file
  1326. #~((string-append "-c" #$config-file))
  1327. #~())
  1328. #$@extra-options)
  1329. #:pid-file #$pid-file))
  1330. (stop #~(make-kill-destructor)))))))
  1331. (define wpa-supplicant-service-type
  1332. (let ((config->package
  1333. (match-lambda
  1334. (($ <wpa-supplicant-configuration> wpa-supplicant)
  1335. (list wpa-supplicant)))))
  1336. (service-type (name 'wpa-supplicant)
  1337. (extensions
  1338. (list (service-extension shepherd-root-service-type
  1339. wpa-supplicant-shepherd-service)
  1340. (service-extension dbus-root-service-type config->package)
  1341. (service-extension profile-service-type config->package)))
  1342. (description "Run the WPA Supplicant daemon, a service that
  1343. implements authentication, key negotiation and more for wireless networks.")
  1344. (default-value (wpa-supplicant-configuration)))))
  1345. ;;;
  1346. ;;; Hostapd.
  1347. ;;;
  1348. (define-record-type* <hostapd-configuration>
  1349. hostapd-configuration make-hostapd-configuration
  1350. hostapd-configuration?
  1351. (package hostapd-configuration-package
  1352. (default hostapd))
  1353. (interface hostapd-configuration-interface ;string
  1354. (default "wlan0"))
  1355. (ssid hostapd-configuration-ssid) ;string
  1356. (broadcast-ssid? hostapd-configuration-broadcast-ssid? ;Boolean
  1357. (default #t))
  1358. (channel hostapd-configuration-channel ;integer
  1359. (default 1))
  1360. (driver hostapd-configuration-driver ;string
  1361. (default "nl80211"))
  1362. ;; See <https://w1.fi/cgit/hostap/plain/hostapd/hostapd.conf> for a list of
  1363. ;; additional options we could add.
  1364. (extra-settings hostapd-configuration-extra-settings ;string
  1365. (default "")))
  1366. (define (hostapd-configuration-file config)
  1367. "Return the configuration file for CONFIG, a <hostapd-configuration>."
  1368. (match-record config <hostapd-configuration>
  1369. (interface ssid broadcast-ssid? channel driver extra-settings)
  1370. (plain-file "hostapd.conf"
  1371. (string-append "\
  1372. # Generated from your Guix configuration.
  1373. interface=" interface "
  1374. ssid=" ssid "
  1375. ignore_broadcast_ssid=" (if broadcast-ssid? "0" "1") "
  1376. channel=" (number->string channel) "\n"
  1377. extra-settings "\n"))))
  1378. (define* (hostapd-shepherd-services config #:key (requirement '()))
  1379. "Return Shepherd services for hostapd."
  1380. (list (shepherd-service
  1381. (provision '(hostapd))
  1382. (requirement `(user-processes ,@requirement))
  1383. (documentation "Run the hostapd WiFi access point daemon.")
  1384. (start #~(make-forkexec-constructor
  1385. (list #$(file-append (hostapd-configuration-package config)
  1386. "/sbin/hostapd")
  1387. #$(hostapd-configuration-file config))
  1388. #:log-file "/var/log/hostapd.log"))
  1389. (stop #~(make-kill-destructor)))))
  1390. (define hostapd-service-type
  1391. (service-type
  1392. (name 'hostapd)
  1393. (extensions
  1394. (list (service-extension shepherd-root-service-type
  1395. hostapd-shepherd-services)))
  1396. (description
  1397. "Run the @uref{https://w1.fi/hostapd/, hostapd} daemon for Wi-Fi access
  1398. points and authentication servers.")))
  1399. (define (simulated-wifi-shepherd-services config)
  1400. "Return Shepherd services to run hostapd with CONFIG, a
  1401. <hostapd-configuration>, as well as services to set up WiFi hardware
  1402. simulation."
  1403. (append (hostapd-shepherd-services config
  1404. #:requirement
  1405. '(unblocked-wifi
  1406. kernel-module-loader))
  1407. (list (shepherd-service
  1408. (provision '(unblocked-wifi))
  1409. (requirement '(file-systems kernel-module-loader))
  1410. (documentation
  1411. "Unblock WiFi devices for use by mac80211_hwsim.")
  1412. (start #~(lambda _
  1413. (invoke #$(file-append util-linux "/sbin/rfkill")
  1414. "unblock" "0")
  1415. (invoke #$(file-append util-linux "/sbin/rfkill")
  1416. "unblock" "1")))
  1417. (one-shot? #t)))))
  1418. (define simulated-wifi-service-type
  1419. (service-type
  1420. (name 'simulated-wifi)
  1421. (extensions
  1422. (list (service-extension shepherd-root-service-type
  1423. simulated-wifi-shepherd-services)
  1424. (service-extension kernel-module-loader-service-type
  1425. (const '("mac80211_hwsim")))))
  1426. (default-value (hostapd-configuration
  1427. (interface "wlan1")
  1428. (ssid "Test Network")))
  1429. (description "Run hostapd to simulate WiFi connectivity.")))
  1430. ;;;
  1431. ;;; Open vSwitch
  1432. ;;;
  1433. (define-record-type* <openvswitch-configuration>
  1434. openvswitch-configuration make-openvswitch-configuration
  1435. openvswitch-configuration?
  1436. (package openvswitch-configuration-package
  1437. (default openvswitch)))
  1438. (define openvswitch-activation
  1439. (match-lambda
  1440. (($ <openvswitch-configuration> package)
  1441. (let ((ovsdb-tool (file-append package "/bin/ovsdb-tool")))
  1442. (with-imported-modules '((guix build utils))
  1443. #~(begin
  1444. (use-modules (guix build utils))
  1445. (mkdir-p "/var/run/openvswitch")
  1446. (mkdir-p "/var/lib/openvswitch")
  1447. (let ((conf.db "/var/lib/openvswitch/conf.db"))
  1448. (unless (file-exists? conf.db)
  1449. (system* #$ovsdb-tool "create" conf.db)))))))))
  1450. (define openvswitch-shepherd-service
  1451. (match-lambda
  1452. (($ <openvswitch-configuration> package)
  1453. (let ((ovsdb-server (file-append package "/sbin/ovsdb-server"))
  1454. (ovs-vswitchd (file-append package "/sbin/ovs-vswitchd")))
  1455. (list
  1456. (shepherd-service
  1457. (provision '(ovsdb))
  1458. (documentation "Run the Open vSwitch database server.")
  1459. (start #~(make-forkexec-constructor
  1460. (list #$ovsdb-server "--pidfile"
  1461. "--remote=punix:/var/run/openvswitch/db.sock")
  1462. #:pid-file "/var/run/openvswitch/ovsdb-server.pid"))
  1463. (stop #~(make-kill-destructor)))
  1464. (shepherd-service
  1465. (provision '(vswitchd))
  1466. (requirement '(ovsdb))
  1467. (documentation "Run the Open vSwitch daemon.")
  1468. (start #~(make-forkexec-constructor
  1469. (list #$ovs-vswitchd "--pidfile")
  1470. #:pid-file "/var/run/openvswitch/ovs-vswitchd.pid"))
  1471. (stop #~(make-kill-destructor))))))))
  1472. (define openvswitch-service-type
  1473. (service-type
  1474. (name 'openvswitch)
  1475. (extensions
  1476. (list (service-extension activation-service-type
  1477. openvswitch-activation)
  1478. (service-extension profile-service-type
  1479. (compose list openvswitch-configuration-package))
  1480. (service-extension shepherd-root-service-type
  1481. openvswitch-shepherd-service)))
  1482. (description
  1483. "Run @uref{http://www.openvswitch.org, Open vSwitch}, a multilayer virtual
  1484. switch designed to enable massive network automation through programmatic
  1485. extension.")
  1486. (default-value (openvswitch-configuration))))
  1487. ;;;
  1488. ;;; iptables
  1489. ;;;
  1490. (define %iptables-accept-all-rules
  1491. (plain-file "iptables-accept-all.rules"
  1492. "*filter
  1493. :INPUT ACCEPT
  1494. :FORWARD ACCEPT
  1495. :OUTPUT ACCEPT
  1496. COMMIT
  1497. "))
  1498. (define-record-type* <iptables-configuration>
  1499. iptables-configuration make-iptables-configuration iptables-configuration?
  1500. (iptables iptables-configuration-iptables
  1501. (default iptables))
  1502. (ipv4-rules iptables-configuration-ipv4-rules
  1503. (default %iptables-accept-all-rules))
  1504. (ipv6-rules iptables-configuration-ipv6-rules
  1505. (default %iptables-accept-all-rules)))
  1506. (define iptables-shepherd-service
  1507. (match-lambda
  1508. (($ <iptables-configuration> iptables ipv4-rules ipv6-rules)
  1509. (let ((iptables-restore (file-append iptables "/sbin/iptables-restore"))
  1510. (ip6tables-restore (file-append iptables "/sbin/ip6tables-restore")))
  1511. (shepherd-service
  1512. (documentation "Packet filtering framework")
  1513. (provision '(iptables))
  1514. (start #~(lambda _
  1515. (invoke #$iptables-restore #$ipv4-rules)
  1516. (invoke #$ip6tables-restore #$ipv6-rules)))
  1517. (stop #~(lambda _
  1518. (invoke #$iptables-restore #$%iptables-accept-all-rules)
  1519. (invoke #$ip6tables-restore #$%iptables-accept-all-rules))))))))
  1520. (define iptables-service-type
  1521. (service-type
  1522. (name 'iptables)
  1523. (description
  1524. "Run @command{iptables-restore}, setting up the specified rules.")
  1525. (extensions
  1526. (list (service-extension shepherd-root-service-type
  1527. (compose list iptables-shepherd-service))))))
  1528. ;;;
  1529. ;;; nftables
  1530. ;;;
  1531. (define %default-nftables-ruleset
  1532. (plain-file "nftables.conf"
  1533. "# A simple and safe firewall
  1534. table inet filter {
  1535. chain input {
  1536. type filter hook input priority 0; policy drop;
  1537. # early drop of invalid connections
  1538. ct state invalid drop
  1539. # allow established/related connections
  1540. ct state { established, related } accept
  1541. # allow from loopback
  1542. iifname lo accept
  1543. # allow icmp
  1544. ip protocol icmp accept
  1545. ip6 nexthdr icmpv6 accept
  1546. # allow ssh
  1547. tcp dport ssh accept
  1548. # reject everything else
  1549. reject with icmpx type port-unreachable
  1550. }
  1551. chain forward {
  1552. type filter hook forward priority 0; policy drop;
  1553. }
  1554. chain output {
  1555. type filter hook output priority 0; policy accept;
  1556. }
  1557. }
  1558. "))
  1559. (define-record-type* <nftables-configuration>
  1560. nftables-configuration
  1561. make-nftables-configuration
  1562. nftables-configuration?
  1563. (package nftables-configuration-package
  1564. (default nftables))
  1565. (ruleset nftables-configuration-ruleset ; file-like object
  1566. (default %default-nftables-ruleset)))
  1567. (define nftables-shepherd-service
  1568. (match-lambda
  1569. (($ <nftables-configuration> package ruleset)
  1570. (let ((nft (file-append package "/sbin/nft")))
  1571. (shepherd-service
  1572. (documentation "Packet filtering and classification")
  1573. (provision '(nftables))
  1574. (start #~(lambda _
  1575. (invoke #$nft "--file" #$ruleset)))
  1576. (stop #~(lambda _
  1577. (invoke #$nft "flush" "ruleset"))))))))
  1578. (define nftables-service-type
  1579. (service-type
  1580. (name 'nftables)
  1581. (description
  1582. "Run @command{nft}, setting up the specified ruleset.")
  1583. (extensions
  1584. (list (service-extension shepherd-root-service-type
  1585. (compose list nftables-shepherd-service))
  1586. (service-extension profile-service-type
  1587. (compose list nftables-configuration-package))))
  1588. (default-value (nftables-configuration))))
  1589. ;;;
  1590. ;;; PageKite
  1591. ;;;
  1592. (define-record-type* <pagekite-configuration>
  1593. pagekite-configuration
  1594. make-pagekite-configuration
  1595. pagekite-configuration?
  1596. (package pagekite-configuration-package
  1597. (default pagekite))
  1598. (kitename pagekite-configuration-kitename
  1599. (default #f))
  1600. (kitesecret pagekite-configuration-kitesecret
  1601. (default #f))
  1602. (frontend pagekite-configuration-frontend
  1603. (default #f))
  1604. (kites pagekite-configuration-kites
  1605. (default '("http:@kitename:localhost:80:@kitesecret")))
  1606. (extra-file pagekite-configuration-extra-file
  1607. (default #f)))
  1608. (define (pagekite-configuration-file config)
  1609. (match-record config <pagekite-configuration>
  1610. (package kitename kitesecret frontend kites extra-file)
  1611. (mixed-text-file "pagekite.rc"
  1612. (if extra-file
  1613. (string-append "optfile = " extra-file "\n")
  1614. "")
  1615. (if kitename
  1616. (string-append "kitename = " kitename "\n")
  1617. "")
  1618. (if kitesecret
  1619. (string-append "kitesecret = " kitesecret "\n")
  1620. "")
  1621. (if frontend
  1622. (string-append "frontend = " frontend "\n")
  1623. "defaults\n")
  1624. (string-join (map (lambda (kite)
  1625. (string-append "service_on = " kite))
  1626. kites)
  1627. "\n"
  1628. 'suffix))))
  1629. (define (pagekite-shepherd-service config)
  1630. (match-record config <pagekite-configuration>
  1631. (package kitename kitesecret frontend kites extra-file)
  1632. (with-imported-modules (source-module-closure
  1633. '((gnu build shepherd)
  1634. (gnu system file-systems)))
  1635. (shepherd-service
  1636. (documentation "Run the PageKite service.")
  1637. (provision '(pagekite))
  1638. (requirement '(networking))
  1639. (modules '((gnu build shepherd)
  1640. (gnu system file-systems)))
  1641. (start #~(make-forkexec-constructor/container
  1642. (list #$(file-append package "/bin/pagekite")
  1643. "--clean"
  1644. "--nullui"
  1645. "--nocrashreport"
  1646. "--runas=pagekite:pagekite"
  1647. (string-append "--optfile="
  1648. #$(pagekite-configuration-file config)))
  1649. #:log-file "/var/log/pagekite.log"
  1650. #:mappings #$(if extra-file
  1651. #~(list (file-system-mapping
  1652. (source #$extra-file)
  1653. (target source)))
  1654. #~'())))
  1655. ;; SIGTERM doesn't always work for some reason.
  1656. (stop #~(make-kill-destructor SIGINT))))))
  1657. (define %pagekite-accounts
  1658. (list (user-group (name "pagekite") (system? #t))
  1659. (user-account
  1660. (name "pagekite")
  1661. (group "pagekite")
  1662. (system? #t)
  1663. (comment "PageKite user")
  1664. (home-directory "/var/empty")
  1665. (shell (file-append shadow "/sbin/nologin")))))
  1666. (define pagekite-service-type
  1667. (service-type
  1668. (name 'pagekite)
  1669. (default-value (pagekite-configuration))
  1670. (extensions
  1671. (list (service-extension shepherd-root-service-type
  1672. (compose list pagekite-shepherd-service))
  1673. (service-extension account-service-type
  1674. (const %pagekite-accounts))))
  1675. (description
  1676. "Run @url{https://pagekite.net/,PageKite}, a tunneling solution to make
  1677. local servers publicly accessible on the web, even behind NATs and firewalls.")))
  1678. ;;;
  1679. ;;; Yggdrasil
  1680. ;;;
  1681. (define-record-type* <yggdrasil-configuration>
  1682. yggdrasil-configuration
  1683. make-yggdrasil-configuration
  1684. yggdrasil-configuration?
  1685. (package yggdrasil-configuration-package
  1686. (default yggdrasil))
  1687. (json-config yggdrasil-configuration-json-config
  1688. (default '()))
  1689. (config-file yggdrasil-config-file
  1690. (default "/etc/yggdrasil-private.conf"))
  1691. (autoconf? yggdrasil-configuration-autoconf?
  1692. (default #f))
  1693. (log-level yggdrasil-configuration-log-level
  1694. (default 'info))
  1695. (log-to yggdrasil-configuration-log-to
  1696. (default 'stdout)))
  1697. (define (yggdrasil-configuration-file config)
  1698. (define (scm->yggdrasil-json x)
  1699. (define key-value?
  1700. dotted-list?)
  1701. (define (param->camel str)
  1702. (string-concatenate
  1703. (map
  1704. string-capitalize
  1705. (string-split str (cut eqv? <> #\-)))))
  1706. (cond
  1707. ((key-value? x)
  1708. (let ((k (car x))
  1709. (v (cdr x)))
  1710. (cons
  1711. (if (symbol? k)
  1712. (param->camel (symbol->string k))
  1713. k)
  1714. v)))
  1715. ((list? x) (map scm->yggdrasil-json x))
  1716. ((vector? x) (vector-map scm->yggdrasil-json x))
  1717. (else x)))
  1718. (computed-file
  1719. "yggdrasil.conf"
  1720. #~(call-with-output-file #$output
  1721. (lambda (port)
  1722. ;; it's HJSON, so comments are a-okay
  1723. (display "# Generated by yggdrasil-service\n" port)
  1724. (display #$(scm->json-string
  1725. (scm->yggdrasil-json
  1726. (yggdrasil-configuration-json-config config)))
  1727. port)))))
  1728. (define (yggdrasil-shepherd-service config)
  1729. "Return a <shepherd-service> for yggdrasil with CONFIG."
  1730. (define yggdrasil-command
  1731. #~(append
  1732. (list (string-append
  1733. #$(yggdrasil-configuration-package config)
  1734. "/bin/yggdrasil")
  1735. "-useconffile"
  1736. #$(yggdrasil-configuration-file config))
  1737. (if #$(yggdrasil-configuration-autoconf? config)
  1738. '("-autoconf")
  1739. '())
  1740. (let ((extraconf #$(yggdrasil-config-file config)))
  1741. (if extraconf
  1742. (list "-extraconffile" extraconf)
  1743. '()))
  1744. (list "-loglevel"
  1745. #$(symbol->string
  1746. (yggdrasil-configuration-log-level config))
  1747. "-logto"
  1748. #$(symbol->string
  1749. (yggdrasil-configuration-log-to config)))))
  1750. (list (shepherd-service
  1751. (documentation "Connect to the Yggdrasil mesh network")
  1752. (provision '(yggdrasil))
  1753. (requirement '(networking))
  1754. (start #~(make-forkexec-constructor
  1755. #$yggdrasil-command
  1756. #:log-file "/var/log/yggdrasil.log"
  1757. #:group "yggdrasil"))
  1758. (stop #~(make-kill-destructor)))))
  1759. (define %yggdrasil-accounts
  1760. (list (user-group (name "yggdrasil") (system? #t))))
  1761. (define yggdrasil-service-type
  1762. (service-type
  1763. (name 'yggdrasil)
  1764. (description
  1765. "Connect to the Yggdrasil mesh network.
  1766. See yggdrasil -genconf for config options.")
  1767. (extensions
  1768. (list (service-extension shepherd-root-service-type
  1769. yggdrasil-shepherd-service)
  1770. (service-extension account-service-type
  1771. (const %yggdrasil-accounts))
  1772. (service-extension profile-service-type
  1773. (compose list yggdrasil-configuration-package))))))
  1774. ;;;
  1775. ;;; IPFS
  1776. ;;;
  1777. (define-record-type* <ipfs-configuration>
  1778. ipfs-configuration
  1779. make-ipfs-configuration
  1780. ipfs-configuration?
  1781. (package ipfs-configuration-package
  1782. (default go-ipfs))
  1783. (gateway ipfs-configuration-gateway
  1784. (default "/ip4/127.0.0.1/tcp/8082"))
  1785. (api ipfs-configuration-api
  1786. (default "/ip4/127.0.0.1/tcp/5001")))
  1787. (define %ipfs-home "/var/lib/ipfs")
  1788. (define %ipfs-accounts
  1789. (list (user-account
  1790. (name "ipfs")
  1791. (group "ipfs")
  1792. (system? #t)
  1793. (comment "IPFS daemon user")
  1794. (home-directory "/var/lib/ipfs")
  1795. (shell (file-append shadow "/sbin/nologin")))
  1796. (user-group
  1797. (name "ipfs")
  1798. (system? #t))))
  1799. (define (ipfs-binary config)
  1800. (file-append (ipfs-configuration-package config) "/bin/ipfs"))
  1801. (define %ipfs-home-mapping
  1802. #~(file-system-mapping
  1803. (source #$%ipfs-home)
  1804. (target #$%ipfs-home)
  1805. (writable? #t)))
  1806. (define %ipfs-environment
  1807. #~(list #$(string-append "HOME=" %ipfs-home)))
  1808. (define (ipfs-shepherd-service config)
  1809. "Return a <shepherd-service> for IPFS with CONFIG."
  1810. (define ipfs-daemon-command
  1811. #~(list #$(ipfs-binary config) "daemon"))
  1812. (list
  1813. (with-imported-modules (source-module-closure
  1814. '((gnu build shepherd)
  1815. (gnu system file-systems)))
  1816. (shepherd-service
  1817. (provision '(ipfs))
  1818. ;; While IPFS is most useful when the machine is connected
  1819. ;; to the network, only loopback is required for starting
  1820. ;; the service.
  1821. (requirement '(loopback))
  1822. (documentation "Connect to the IPFS network")
  1823. (modules '((gnu build shepherd)
  1824. (gnu system file-systems)))
  1825. (start #~(make-forkexec-constructor/container
  1826. #$ipfs-daemon-command
  1827. #:namespaces '#$(fold delq %namespaces '(user net))
  1828. #:mappings (list #$%ipfs-home-mapping)
  1829. #:log-file "/var/log/ipfs.log"
  1830. #:user "ipfs"
  1831. #:group "ipfs"
  1832. #:environment-variables #$%ipfs-environment))
  1833. (stop #~(make-kill-destructor))))))
  1834. (define (%ipfs-activation config)
  1835. "Return an activation gexp for IPFS with CONFIG"
  1836. (define (ipfs-config-command setting value)
  1837. #~(#$(ipfs-binary config) "config" #$setting #$value))
  1838. (define (set-config!-gexp setting value)
  1839. #~(system* #$@(ipfs-config-command setting value)))
  1840. (define settings
  1841. `(("Addresses.API" ,(ipfs-configuration-api config))
  1842. ("Addresses.Gateway" ,(ipfs-configuration-gateway config))))
  1843. (define inner-gexp
  1844. #~(begin
  1845. (umask #o077)
  1846. ;; Create $HOME/.ipfs structure
  1847. (system* #$(ipfs-binary config) "init")
  1848. ;; Apply settings
  1849. #$@(map (cute apply set-config!-gexp <>) settings)))
  1850. (define inner-script
  1851. (program-file "ipfs-activation-inner" inner-gexp))
  1852. ;; Run ipfs init and ipfs config from a container,
  1853. ;; in case the IPFS daemon was compromised at some point
  1854. ;; and ~/.ipfs is now a symlink to somewhere outside
  1855. ;; %ipfs-home.
  1856. (define container-gexp
  1857. (with-extensions (list shepherd)
  1858. (with-imported-modules (source-module-closure
  1859. '((gnu build shepherd)
  1860. (gnu system file-systems)))
  1861. #~(begin
  1862. (use-modules (gnu build shepherd)
  1863. (gnu system file-systems))
  1864. (let* ((constructor
  1865. (make-forkexec-constructor/container
  1866. (list #$inner-script)
  1867. #:namespaces '#$(fold delq %namespaces '(user))
  1868. #:mappings (list #$%ipfs-home-mapping)
  1869. #:user "ipfs"
  1870. #:group "ipfs"
  1871. #:environment-variables #$%ipfs-environment))
  1872. (pid (constructor)))
  1873. (waitpid pid))))))
  1874. ;; The activation may happen from the initrd, which uses
  1875. ;; a statically-linked guile, while the guix container
  1876. ;; procedures require a working dynamic-link.
  1877. (define container-script
  1878. (program-file "ipfs-activation-container" container-gexp))
  1879. #~(system* #$container-script))
  1880. (define ipfs-service-type
  1881. (service-type
  1882. (name 'ipfs)
  1883. (extensions
  1884. (list (service-extension account-service-type
  1885. (const %ipfs-accounts))
  1886. (service-extension activation-service-type
  1887. %ipfs-activation)
  1888. (service-extension shepherd-root-service-type
  1889. ipfs-shepherd-service)))
  1890. (default-value (ipfs-configuration))
  1891. (description
  1892. "Run @command{ipfs daemon}, the reference implementation
  1893. of the IPFS peer-to-peer storage network.")))
  1894. ;;;
  1895. ;;; Keepalived
  1896. ;;;
  1897. (define-record-type* <keepalived-configuration>
  1898. keepalived-configuration make-keepalived-configuration
  1899. keepalived-configuration?
  1900. (keepalived keepalived-configuration-keepalived ;<package>
  1901. (default keepalived))
  1902. (config-file keepalived-configuration-config-file ;file-like
  1903. (default #f)))
  1904. (define keepalived-shepherd-service
  1905. (match-lambda
  1906. (($ <keepalived-configuration> keepalived config-file)
  1907. (list
  1908. (shepherd-service
  1909. (provision '(keepalived))
  1910. (documentation "Run keepalived.")
  1911. (requirement '(loopback))
  1912. (start #~(make-forkexec-constructor
  1913. (list (string-append #$keepalived "/sbin/keepalived")
  1914. "--dont-fork" "--log-console" "--log-detail"
  1915. "--pid=/var/run/keepalived.pid"
  1916. (string-append "--use-file=" #$config-file))
  1917. #:pid-file "/var/run/keepalived.pid"
  1918. #:log-file "/var/log/keepalived.log"))
  1919. (respawn? #f)
  1920. (stop #~(make-kill-destructor)))))))
  1921. (define keepalived-service-type
  1922. (service-type (name 'keepalived)
  1923. (extensions (list (service-extension shepherd-root-service-type
  1924. keepalived-shepherd-service)))
  1925. (description
  1926. "Run @uref{https://www.keepalived.org/, Keepalived}
  1927. routing software.")))
  1928. ;;; networking.scm ends here