networking.scm 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919
  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 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 Christopher Lemmer Webber <cwebber@dustycloud.org>
  19. ;;;
  20. ;;; This file is part of GNU Guix.
  21. ;;;
  22. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  23. ;;; under the terms of the GNU General Public License as published by
  24. ;;; the Free Software Foundation; either version 3 of the License, or (at
  25. ;;; your option) any later version.
  26. ;;;
  27. ;;; GNU Guix is distributed in the hope that it will be useful, but
  28. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  29. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. ;;; GNU General Public License for more details.
  31. ;;;
  32. ;;; You should have received a copy of the GNU General Public License
  33. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  34. (define-module (gnu services networking)
  35. #:use-module (gnu services)
  36. #:use-module (gnu services base)
  37. #:use-module (gnu services configuration)
  38. #:use-module (gnu services linux)
  39. #:use-module (gnu services shepherd)
  40. #:use-module (gnu services dbus)
  41. #:use-module (gnu system shadow)
  42. #:use-module (gnu system pam)
  43. #:use-module (gnu packages admin)
  44. #:use-module (gnu packages base)
  45. #:use-module (gnu packages bash)
  46. #:use-module (gnu packages cluster)
  47. #:use-module (gnu packages connman)
  48. #:use-module (gnu packages freedesktop)
  49. #:use-module (gnu packages linux)
  50. #:use-module (gnu packages tor)
  51. #:use-module (gnu packages usb-modeswitch)
  52. #:use-module (gnu packages messaging)
  53. #:use-module (gnu packages networking)
  54. #:use-module (gnu packages ntp)
  55. #:use-module (gnu packages wicd)
  56. #:use-module (gnu packages gnome)
  57. #:use-module (guix gexp)
  58. #:use-module (guix records)
  59. #:use-module (guix modules)
  60. #:use-module (guix packages)
  61. #:use-module (guix deprecation)
  62. #:use-module (rnrs enums)
  63. #:use-module (srfi srfi-1)
  64. #:use-module (srfi srfi-9)
  65. #:use-module (srfi srfi-26)
  66. #:use-module (srfi srfi-43)
  67. #:use-module (ice-9 match)
  68. #:use-module (json)
  69. #:re-export (static-networking-service
  70. static-networking-service-type)
  71. #:export (%facebook-host-aliases
  72. dhcp-client-service
  73. dhcp-client-service-type
  74. dhcpd-service-type
  75. dhcpd-configuration
  76. dhcpd-configuration?
  77. dhcpd-configuration-package
  78. dhcpd-configuration-config-file
  79. dhcpd-configuration-version
  80. dhcpd-configuration-run-directory
  81. dhcpd-configuration-lease-file
  82. dhcpd-configuration-pid-file
  83. dhcpd-configuration-interfaces
  84. ntp-configuration
  85. ntp-configuration?
  86. ntp-configuration-ntp
  87. ntp-configuration-servers
  88. ntp-allow-large-adjustment?
  89. %ntp-servers
  90. ntp-server
  91. ntp-server-type
  92. ntp-server-address
  93. ntp-server-options
  94. ntp-service
  95. ntp-service-type
  96. %openntpd-servers
  97. openntpd-configuration
  98. openntpd-configuration?
  99. openntpd-service-type
  100. inetd-configuration
  101. inetd-entry
  102. inetd-service-type
  103. tor-configuration
  104. tor-configuration?
  105. tor-hidden-service
  106. tor-service
  107. tor-service-type
  108. wicd-service-type
  109. wicd-service
  110. network-manager-configuration
  111. network-manager-configuration?
  112. network-manager-configuration-dns
  113. network-manager-configuration-vpn-plugins
  114. network-manager-service-type
  115. connman-configuration
  116. connman-configuration?
  117. connman-service-type
  118. modem-manager-configuration
  119. modem-manager-configuration?
  120. modem-manager-service-type
  121. usb-modeswitch-configuration
  122. usb-modeswitch-configuration?
  123. usb-modeswitch-configuration-usb-modeswitch
  124. usb-modeswitch-configuration-usb-modeswitch-data
  125. usb-modeswitch-service-type
  126. wpa-supplicant-configuration
  127. wpa-supplicant-configuration?
  128. wpa-supplicant-configuration-wpa-supplicant
  129. wpa-supplicant-configuration-requirement
  130. wpa-supplicant-configuration-pid-file
  131. wpa-supplicant-configuration-dbus?
  132. wpa-supplicant-configuration-interface
  133. wpa-supplicant-configuration-config-file
  134. wpa-supplicant-configuration-extra-options
  135. wpa-supplicant-service-type
  136. hostapd-configuration
  137. hostapd-configuration?
  138. hostapd-configuration-package
  139. hostapd-configuration-interface
  140. hostapd-configuration-ssid
  141. hostapd-configuration-broadcast-ssid?
  142. hostapd-configuration-channel
  143. hostapd-configuration-driver
  144. hostapd-service-type
  145. simulated-wifi-service-type
  146. openvswitch-service-type
  147. openvswitch-configuration
  148. iptables-configuration
  149. iptables-configuration?
  150. iptables-configuration-iptables
  151. iptables-configuration-ipv4-rules
  152. iptables-configuration-ipv6-rules
  153. iptables-service-type
  154. nftables-service-type
  155. nftables-configuration
  156. nftables-configuration?
  157. nftables-configuration-package
  158. nftables-configuration-ruleset
  159. %default-nftables-ruleset
  160. pagekite-service-type
  161. pagekite-configuration
  162. pagekite-configuration?
  163. pagekite-configuration-package
  164. pagekite-configuration-kitename
  165. pagekite-configuration-kitesecret
  166. pagekite-configuration-frontend
  167. pagekite-configuration-kites
  168. pagekite-configuration-extra-file
  169. yggdrasil-service-type
  170. yggdrasil-configuration
  171. yggdrasil-configuration?
  172. yggdrasil-configuration-autoconf?
  173. yggdrasil-configuration-config-file
  174. yggdrasil-configuration-log-level
  175. yggdrasil-configuration-log-to
  176. yggdrasil-configuration-json-config
  177. yggdrasil-configuration-package
  178. keepalived-configuration
  179. keepalived-configuration?
  180. keepalived-service-type))
  181. ;;; Commentary:
  182. ;;;
  183. ;;; Networking services.
  184. ;;;
  185. ;;; Code:
  186. (define %facebook-host-aliases
  187. ;; This is the list of known Facebook hosts to be added to /etc/hosts if you
  188. ;; are to block it.
  189. "\
  190. # Block Facebook IPv4.
  191. 127.0.0.1 www.facebook.com
  192. 127.0.0.1 facebook.com
  193. 127.0.0.1 login.facebook.com
  194. 127.0.0.1 www.login.facebook.com
  195. 127.0.0.1 fbcdn.net
  196. 127.0.0.1 www.fbcdn.net
  197. 127.0.0.1 fbcdn.com
  198. 127.0.0.1 www.fbcdn.com
  199. 127.0.0.1 static.ak.fbcdn.net
  200. 127.0.0.1 static.ak.connect.facebook.com
  201. 127.0.0.1 connect.facebook.net
  202. 127.0.0.1 www.connect.facebook.net
  203. 127.0.0.1 apps.facebook.com
  204. # Block Facebook IPv6.
  205. fe80::1%lo0 facebook.com
  206. fe80::1%lo0 login.facebook.com
  207. fe80::1%lo0 www.login.facebook.com
  208. fe80::1%lo0 fbcdn.net
  209. fe80::1%lo0 www.fbcdn.net
  210. fe80::1%lo0 fbcdn.com
  211. fe80::1%lo0 www.fbcdn.com
  212. fe80::1%lo0 static.ak.fbcdn.net
  213. fe80::1%lo0 static.ak.connect.facebook.com
  214. fe80::1%lo0 connect.facebook.net
  215. fe80::1%lo0 www.connect.facebook.net
  216. fe80::1%lo0 apps.facebook.com\n")
  217. (define dhcp-client-service-type
  218. (shepherd-service-type
  219. 'dhcp-client
  220. (lambda (dhcp)
  221. (define dhclient
  222. (file-append dhcp "/sbin/dhclient"))
  223. (define pid-file
  224. "/var/run/dhclient.pid")
  225. (shepherd-service
  226. (documentation "Set up networking via DHCP.")
  227. (requirement '(user-processes udev))
  228. ;; XXX: Running with '-nw' ("no wait") avoids blocking for a minute when
  229. ;; networking is unavailable, but also means that the interface is not up
  230. ;; yet when 'start' completes. To wait for the interface to be ready, one
  231. ;; should instead monitor udev events.
  232. (provision '(networking))
  233. (start #~(lambda _
  234. ;; When invoked without any arguments, 'dhclient' discovers all
  235. ;; non-loopback interfaces *that are up*. However, the relevant
  236. ;; interfaces are typically down at this point. Thus we perform
  237. ;; our own interface discovery here.
  238. (define valid?
  239. (lambda (interface)
  240. (and (arp-network-interface? interface)
  241. (not (loopback-network-interface? interface))
  242. ;; XXX: Make sure the interfaces are up so that
  243. ;; 'dhclient' can actually send/receive over them.
  244. ;; Ignore those that cannot be activated.
  245. (false-if-exception
  246. (set-network-interface-up interface)))))
  247. (define ifaces
  248. (filter valid? (all-network-interface-names)))
  249. (false-if-exception (delete-file #$pid-file))
  250. (let ((pid (fork+exec-command
  251. (cons* #$dhclient "-nw"
  252. "-pf" #$pid-file ifaces))))
  253. (and (zero? (cdr (waitpid pid)))
  254. (read-pid-file #$pid-file)))))
  255. (stop #~(make-kill-destructor))))
  256. isc-dhcp
  257. (description "Run @command{dhcp}, a Dynamic Host Configuration
  258. Protocol (DHCP) client, on all the non-loopback network interfaces.")))
  259. (define-deprecated (dhcp-client-service #:key (dhcp isc-dhcp))
  260. dhcp-client-service-type
  261. "Return a service that runs @var{dhcp}, a Dynamic Host Configuration
  262. Protocol (DHCP) client, on all the non-loopback network interfaces."
  263. (service dhcp-client-service-type dhcp))
  264. (define-record-type* <dhcpd-configuration>
  265. dhcpd-configuration make-dhcpd-configuration
  266. dhcpd-configuration?
  267. (package dhcpd-configuration-package ;<package>
  268. (default isc-dhcp))
  269. (config-file dhcpd-configuration-config-file ;file-like
  270. (default #f))
  271. (version dhcpd-configuration-version ;"4", "6", or "4o6"
  272. (default "4"))
  273. (run-directory dhcpd-configuration-run-directory
  274. (default "/run/dhcpd"))
  275. (lease-file dhcpd-configuration-lease-file
  276. (default "/var/db/dhcpd.leases"))
  277. (pid-file dhcpd-configuration-pid-file
  278. (default "/run/dhcpd/dhcpd.pid"))
  279. ;; list of strings, e.g. (list "enp0s25")
  280. (interfaces dhcpd-configuration-interfaces
  281. (default '())))
  282. (define dhcpd-shepherd-service
  283. (match-lambda
  284. (($ <dhcpd-configuration> package config-file version run-directory
  285. lease-file pid-file interfaces)
  286. (unless config-file
  287. (error "Must supply a config-file"))
  288. (list (shepherd-service
  289. ;; Allow users to easily run multiple versions simultaneously.
  290. (provision (list (string->symbol
  291. (string-append "dhcpv" version "-daemon"))))
  292. (documentation (string-append "Run the DHCPv" version " daemon"))
  293. (requirement '(networking))
  294. (start #~(make-forkexec-constructor
  295. '(#$(file-append package "/sbin/dhcpd")
  296. #$(string-append "-" version)
  297. "-lf" #$lease-file
  298. "-pf" #$pid-file
  299. "-cf" #$config-file
  300. #$@interfaces)
  301. #:pid-file #$pid-file))
  302. (stop #~(make-kill-destructor)))))))
  303. (define dhcpd-activation
  304. (match-lambda
  305. (($ <dhcpd-configuration> package config-file version run-directory
  306. lease-file pid-file interfaces)
  307. (with-imported-modules '((guix build utils))
  308. #~(begin
  309. (unless (file-exists? #$run-directory)
  310. (mkdir #$run-directory))
  311. ;; According to the DHCP manual (man dhcpd.leases), the lease
  312. ;; database must be present for dhcpd to start successfully.
  313. (unless (file-exists? #$lease-file)
  314. (with-output-to-file #$lease-file
  315. (lambda _ (display ""))))
  316. ;; Validate the config.
  317. (invoke/quiet
  318. #$(file-append package "/sbin/dhcpd") "-t" "-cf"
  319. #$config-file))))))
  320. (define dhcpd-service-type
  321. (service-type
  322. (name 'dhcpd)
  323. (extensions
  324. (list (service-extension shepherd-root-service-type dhcpd-shepherd-service)
  325. (service-extension activation-service-type dhcpd-activation)))
  326. (description "Run a DHCP (Dynamic Host Configuration Protocol) daemon. The
  327. daemon is responsible for allocating IP addresses to its client.")))
  328. ;;;
  329. ;;; NTP.
  330. ;;;
  331. (define ntp-server-types (make-enumeration
  332. '(pool
  333. server
  334. peer
  335. broadcast
  336. manycastclient)))
  337. (define-record-type* <ntp-server>
  338. ntp-server make-ntp-server
  339. ntp-server?
  340. ;; The type can be one of the symbols of the NTP-SERVER-TYPE? enumeration.
  341. (type ntp-server-type
  342. (default 'server))
  343. (address ntp-server-address) ; a string
  344. ;; The list of options can contain single option names or tuples in the form
  345. ;; '(name value).
  346. (options ntp-server-options
  347. (default '())))
  348. (define (ntp-server->string ntp-server)
  349. ;; Serialize the NTP server object as a string, ready to use in the NTP
  350. ;; configuration file.
  351. (define (flatten lst)
  352. (reverse
  353. (let loop ((x lst)
  354. (res '()))
  355. (if (list? x)
  356. (fold loop res x)
  357. (cons (format #f "~a" x) res)))))
  358. (match ntp-server
  359. (($ <ntp-server> type address options)
  360. ;; XXX: It'd be neater if fields were validated at the syntax level (for
  361. ;; static ones at least). Perhaps the Guix record type could support a
  362. ;; predicate property on a field?
  363. (unless (enum-set-member? type ntp-server-types)
  364. (error "Invalid NTP server type" type))
  365. (string-join (cons* (symbol->string type)
  366. address
  367. (flatten options))))))
  368. (define %ntp-servers
  369. ;; Default set of NTP servers. These URLs are managed by the NTP Pool project.
  370. ;; Within Guix, Leo Famulari <leo@famulari.name> is the administrative contact
  371. ;; for this NTP pool "zone".
  372. (list
  373. (ntp-server
  374. (type 'pool)
  375. (address "0.guix.pool.ntp.org")
  376. (options '("iburst"))))) ;as recommended in the ntpd manual
  377. (define-record-type* <ntp-configuration>
  378. ntp-configuration make-ntp-configuration
  379. ntp-configuration?
  380. (ntp ntp-configuration-ntp
  381. (default ntp))
  382. (servers %ntp-configuration-servers ;list of <ntp-server> objects
  383. (default %ntp-servers))
  384. (allow-large-adjustment? ntp-allow-large-adjustment?
  385. (default #t))) ;as recommended in the ntpd manual
  386. (define (ntp-configuration-servers ntp-configuration)
  387. ;; A wrapper to support the deprecated form of this field.
  388. (let ((ntp-servers (%ntp-configuration-servers ntp-configuration)))
  389. (match ntp-servers
  390. (((? string?) (? string?) ...)
  391. (format (current-error-port) "warning: Defining NTP servers as strings is \
  392. deprecated. Please use <ntp-server> records instead.\n")
  393. (map (lambda (addr)
  394. (ntp-server
  395. (type 'server)
  396. (address addr)
  397. (options '()))) ntp-servers))
  398. ((($ <ntp-server>) ($ <ntp-server>) ...)
  399. ntp-servers))))
  400. (define ntp-shepherd-service
  401. (lambda (config)
  402. (match config
  403. (($ <ntp-configuration> ntp servers allow-large-adjustment?)
  404. (let ((servers (ntp-configuration-servers config)))
  405. ;; TODO: Add authentication support.
  406. (define config
  407. (string-append "driftfile /var/run/ntpd/ntp.drift\n"
  408. (string-join (map ntp-server->string servers)
  409. "\n")
  410. "
  411. # Disable status queries as a workaround for CVE-2013-5211:
  412. # <http://support.ntp.org/bin/view/Main/SecurityNotice#DRDoS_Amplification_Attack_using>.
  413. restrict default kod nomodify notrap nopeer noquery limited
  414. restrict -6 default kod nomodify notrap nopeer noquery limited
  415. # Yet, allow use of the local 'ntpq'.
  416. restrict 127.0.0.1
  417. restrict -6 ::1
  418. # This is required to use servers from a pool directive when using the 'nopeer'
  419. # option by default, as documented in the 'ntp.conf' manual.
  420. restrict source notrap nomodify noquery\n"))
  421. (define ntpd.conf
  422. (plain-file "ntpd.conf" config))
  423. (list (shepherd-service
  424. (provision '(ntpd))
  425. (documentation "Run the Network Time Protocol (NTP) daemon.")
  426. (requirement '(user-processes networking))
  427. (start #~(make-forkexec-constructor
  428. (list (string-append #$ntp "/bin/ntpd") "-n"
  429. "-c" #$ntpd.conf "-u" "ntpd"
  430. #$@(if allow-large-adjustment?
  431. '("-g")
  432. '()))))
  433. (stop #~(make-kill-destructor)))))))))
  434. (define %ntp-accounts
  435. (list (user-account
  436. (name "ntpd")
  437. (group "nogroup")
  438. (system? #t)
  439. (comment "NTP daemon user")
  440. (home-directory "/var/empty")
  441. (shell (file-append shadow "/sbin/nologin")))))
  442. (define (ntp-service-activation config)
  443. "Return the activation gexp for CONFIG."
  444. (with-imported-modules '((guix build utils))
  445. #~(begin
  446. (use-modules (guix build utils))
  447. (define %user
  448. (getpw "ntpd"))
  449. (let ((directory "/var/run/ntpd"))
  450. (mkdir-p directory)
  451. (chown directory (passwd:uid %user) (passwd:gid %user))))))
  452. (define ntp-service-type
  453. (service-type (name 'ntp)
  454. (extensions
  455. (list (service-extension shepherd-root-service-type
  456. ntp-shepherd-service)
  457. (service-extension account-service-type
  458. (const %ntp-accounts))
  459. (service-extension activation-service-type
  460. ntp-service-activation)))
  461. (description
  462. "Run the @command{ntpd}, the Network Time Protocol (NTP)
  463. daemon of the @uref{http://www.ntp.org, Network Time Foundation}. The daemon
  464. will keep the system clock synchronized with that of the given servers.")
  465. (default-value (ntp-configuration))))
  466. (define-deprecated (ntp-service #:key (ntp ntp)
  467. (servers %ntp-servers)
  468. allow-large-adjustment?)
  469. ntp-service-type
  470. "Return a service that runs the daemon from @var{ntp}, the
  471. @uref{http://www.ntp.org, Network Time Protocol package}. The daemon will
  472. keep the system clock synchronized with that of @var{servers}.
  473. @var{allow-large-adjustment?} determines whether @command{ntpd} is allowed to
  474. make an initial adjustment of more than 1,000 seconds."
  475. (service ntp-service-type
  476. (ntp-configuration (ntp ntp)
  477. (servers servers)
  478. (allow-large-adjustment?
  479. allow-large-adjustment?))))
  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. ;;; Tor.
  654. ;;;
  655. (define-record-type* <tor-configuration>
  656. tor-configuration make-tor-configuration
  657. tor-configuration?
  658. (tor tor-configuration-tor
  659. (default tor))
  660. (config-file tor-configuration-config-file
  661. (default (plain-file "empty" "")))
  662. (hidden-services tor-configuration-hidden-services
  663. (default '()))
  664. (socks-socket-type tor-configuration-socks-socket-type ; 'tcp or 'unix
  665. (default 'tcp))
  666. (control-socket? tor-control-socket-path
  667. (default #f)))
  668. (define %tor-accounts
  669. ;; User account and groups for Tor.
  670. (list (user-group (name "tor") (system? #t))
  671. (user-account
  672. (name "tor")
  673. (group "tor")
  674. (system? #t)
  675. (comment "Tor daemon user")
  676. (home-directory "/var/empty")
  677. (shell (file-append shadow "/sbin/nologin")))))
  678. (define-record-type <hidden-service>
  679. (hidden-service name mapping)
  680. hidden-service?
  681. (name hidden-service-name) ;string
  682. (mapping hidden-service-mapping)) ;list of port/address tuples
  683. (define (tor-configuration->torrc config)
  684. "Return a 'torrc' file for CONFIG."
  685. (match config
  686. (($ <tor-configuration> tor config-file services
  687. socks-socket-type control-socket?)
  688. (computed-file
  689. "torrc"
  690. (with-imported-modules '((guix build utils))
  691. #~(begin
  692. (use-modules (guix build utils)
  693. (ice-9 match))
  694. (call-with-output-file #$output
  695. (lambda (port)
  696. (display "\
  697. ### These lines were generated from your system configuration:
  698. User tor
  699. DataDirectory /var/lib/tor
  700. PidFile /var/run/tor/tor.pid
  701. Log notice syslog\n" port)
  702. (when (eq? 'unix '#$socks-socket-type)
  703. (display "\
  704. SocksPort unix:/var/run/tor/socks-sock
  705. UnixSocksGroupWritable 1\n" port))
  706. (when #$control-socket?
  707. (display "\
  708. ControlSocket unix:/var/run/tor/control-sock GroupWritable RelaxDirModeCheck
  709. ControlSocketsGroupWritable 1\n"
  710. port))
  711. (for-each (match-lambda
  712. ((service (ports hosts) ...)
  713. (format port "\
  714. HiddenServiceDir /var/lib/tor/hidden-services/~a~%"
  715. service)
  716. (for-each (lambda (tcp-port host)
  717. (format port "\
  718. HiddenServicePort ~a ~a~%"
  719. tcp-port host))
  720. ports hosts)))
  721. '#$(map (match-lambda
  722. (($ <hidden-service> name mapping)
  723. (cons name mapping)))
  724. services))
  725. (display "\
  726. ### End of automatically generated lines.\n\n" port)
  727. ;; Append the user's config file.
  728. (call-with-input-file #$config-file
  729. (lambda (input)
  730. (dump-port input port)))
  731. #t))))))))
  732. (define (tor-shepherd-service config)
  733. "Return a <shepherd-service> running Tor."
  734. (match config
  735. (($ <tor-configuration> tor)
  736. (let ((torrc (tor-configuration->torrc config)))
  737. (with-imported-modules (source-module-closure
  738. '((gnu build shepherd)
  739. (gnu system file-systems)))
  740. (list (shepherd-service
  741. (provision '(tor))
  742. ;; Tor needs at least one network interface to be up, hence the
  743. ;; dependency on 'loopback'.
  744. (requirement '(user-processes loopback syslogd))
  745. (modules '((gnu build shepherd)
  746. (gnu system file-systems)))
  747. (start #~(make-forkexec-constructor/container
  748. (list #$(file-append tor "/bin/tor") "-f" #$torrc)
  749. #:mappings (list (file-system-mapping
  750. (source "/var/lib/tor")
  751. (target source)
  752. (writable? #t))
  753. (file-system-mapping
  754. (source "/dev/log") ;for syslog
  755. (target source))
  756. (file-system-mapping
  757. (source "/var/run/tor")
  758. (target source)
  759. (writable? #t)))
  760. #:pid-file "/var/run/tor/tor.pid"))
  761. (stop #~(make-kill-destructor))
  762. (documentation "Run the Tor anonymous network overlay."))))))))
  763. (define (tor-activation config)
  764. "Set up directories for Tor and its hidden services, if any."
  765. #~(begin
  766. (use-modules (guix build utils))
  767. (define %user
  768. (getpw "tor"))
  769. (define (initialize service)
  770. (let ((directory (string-append "/var/lib/tor/hidden-services/"
  771. service)))
  772. (mkdir-p directory)
  773. (chown directory (passwd:uid %user) (passwd:gid %user))
  774. ;; The daemon bails out if we give wider permissions.
  775. (chmod directory #o700)))
  776. ;; Allow Tor to write its PID file.
  777. (mkdir-p "/var/run/tor")
  778. (chown "/var/run/tor" (passwd:uid %user) (passwd:gid %user))
  779. ;; Set the group permissions to rw so that if the system administrator
  780. ;; has specified UnixSocksGroupWritable=1 in their torrc file, members
  781. ;; of the "tor" group will be able to use the SOCKS socket.
  782. (chmod "/var/run/tor" #o750)
  783. ;; Allow Tor to access the hidden services' directories.
  784. (mkdir-p "/var/lib/tor")
  785. (chown "/var/lib/tor" (passwd:uid %user) (passwd:gid %user))
  786. (chmod "/var/lib/tor" #o700)
  787. ;; Make sure /var/lib is accessible to the 'tor' user.
  788. (chmod "/var/lib" #o755)
  789. (for-each initialize
  790. '#$(map hidden-service-name
  791. (tor-configuration-hidden-services config)))))
  792. (define tor-service-type
  793. (service-type (name 'tor)
  794. (extensions
  795. (list (service-extension shepherd-root-service-type
  796. tor-shepherd-service)
  797. (service-extension account-service-type
  798. (const %tor-accounts))
  799. (service-extension activation-service-type
  800. tor-activation)))
  801. ;; This can be extended with hidden services.
  802. (compose concatenate)
  803. (extend (lambda (config services)
  804. (tor-configuration
  805. (inherit config)
  806. (hidden-services
  807. (append (tor-configuration-hidden-services config)
  808. services)))))
  809. (default-value (tor-configuration))
  810. (description
  811. "Run the @uref{https://torproject.org, Tor} anonymous
  812. networking daemon.")))
  813. (define-deprecated (tor-service #:optional
  814. (config-file (plain-file "empty" ""))
  815. #:key (tor tor))
  816. tor-service-type
  817. "Return a service to run the @uref{https://torproject.org, Tor} anonymous
  818. networking daemon.
  819. The daemon runs as the @code{tor} unprivileged user. It is passed
  820. @var{config-file}, a file-like object, with an additional @code{User tor} line
  821. and lines for hidden services added via @code{tor-hidden-service}. Run
  822. @command{man tor} for information about the configuration file."
  823. (service tor-service-type
  824. (tor-configuration (tor tor)
  825. (config-file config-file))))
  826. (define tor-hidden-service-type
  827. ;; A type that extends Tor with hidden services.
  828. (service-type (name 'tor-hidden-service)
  829. (extensions
  830. (list (service-extension tor-service-type list)))
  831. (description
  832. "Define a new Tor @dfn{hidden service}.")))
  833. (define (tor-hidden-service name mapping)
  834. "Define a new Tor @dfn{hidden service} called @var{name} and implementing
  835. @var{mapping}. @var{mapping} is a list of port/host tuples, such as:
  836. @example
  837. '((22 \"127.0.0.1:22\")
  838. (80 \"127.0.0.1:8080\"))
  839. @end example
  840. In this example, port 22 of the hidden service is mapped to local port 22, and
  841. port 80 is mapped to local port 8080.
  842. This creates a @file{/var/lib/tor/hidden-services/@var{name}} directory, where
  843. the @file{hostname} file contains the @code{.onion} host name for the hidden
  844. service.
  845. See @uref{https://www.torproject.org/docs/tor-hidden-service.html.en, the Tor
  846. project's documentation} for more information."
  847. (service tor-hidden-service-type
  848. (hidden-service name mapping)))
  849. ;;;
  850. ;;; Wicd.
  851. ;;;
  852. (define %wicd-activation
  853. ;; Activation gexp for Wicd.
  854. #~(begin
  855. (use-modules (guix build utils))
  856. (mkdir-p "/etc/wicd")
  857. (let ((file-name "/etc/wicd/dhclient.conf.template.default"))
  858. (unless (file-exists? file-name)
  859. (copy-file (string-append #$wicd file-name)
  860. file-name)))
  861. ;; Wicd invokes 'wpa_supplicant', which needs this directory for its
  862. ;; named socket files.
  863. (mkdir-p "/var/run/wpa_supplicant")
  864. (chmod "/var/run/wpa_supplicant" #o750)))
  865. (define (wicd-shepherd-service wicd)
  866. "Return a shepherd service for WICD."
  867. (list (shepherd-service
  868. (documentation "Run the Wicd network manager.")
  869. (provision '(networking))
  870. (requirement '(user-processes dbus-system loopback))
  871. (start #~(make-forkexec-constructor
  872. (list (string-append #$wicd "/sbin/wicd")
  873. "--no-daemon")))
  874. (stop #~(make-kill-destructor)))))
  875. (define wicd-service-type
  876. (service-type (name 'wicd)
  877. (extensions
  878. (list (service-extension shepherd-root-service-type
  879. wicd-shepherd-service)
  880. (service-extension dbus-root-service-type
  881. list)
  882. (service-extension activation-service-type
  883. (const %wicd-activation))
  884. ;; Add Wicd to the global profile.
  885. (service-extension profile-service-type list)))
  886. (description
  887. "Run @url{https://launchpad.net/wicd,Wicd}, a network
  888. management daemon that aims to simplify wired and wireless networking.")))
  889. (define* (wicd-service #:key (wicd wicd))
  890. "Return a service that runs @url{https://launchpad.net/wicd,Wicd}, a network
  891. management daemon that aims to simplify wired and wireless networking.
  892. This service adds the @var{wicd} package to the global profile, providing
  893. several commands to interact with the daemon and configure networking:
  894. @command{wicd-client}, a graphical user interface, and the @command{wicd-cli}
  895. and @command{wicd-curses} user interfaces."
  896. (service wicd-service-type wicd))
  897. ;;;
  898. ;;; ModemManager
  899. ;;;
  900. (define-record-type* <modem-manager-configuration>
  901. modem-manager-configuration make-modem-manager-configuration
  902. modem-manager-configuration?
  903. (modem-manager modem-manager-configuration-modem-manager
  904. (default modem-manager)))
  905. ;;;
  906. ;;; NetworkManager
  907. ;;;
  908. (define-record-type* <network-manager-configuration>
  909. network-manager-configuration make-network-manager-configuration
  910. network-manager-configuration?
  911. (network-manager network-manager-configuration-network-manager
  912. (default network-manager))
  913. (dns network-manager-configuration-dns
  914. (default "default"))
  915. (vpn-plugins network-manager-configuration-vpn-plugins ;list of <package>
  916. (default '())))
  917. (define network-manager-activation
  918. ;; Activation gexp for NetworkManager
  919. (match-lambda
  920. (($ <network-manager-configuration> network-manager dns vpn-plugins)
  921. #~(begin
  922. (use-modules (guix build utils))
  923. (mkdir-p "/etc/NetworkManager/system-connections")
  924. #$@(if (equal? dns "dnsmasq")
  925. ;; create directory to store dnsmasq lease file
  926. '((mkdir-p "/var/lib/misc"))
  927. '())))))
  928. (define (vpn-plugin-directory plugins)
  929. "Return a directory containing PLUGINS, the NM VPN plugins."
  930. (directory-union "network-manager-vpn-plugins" plugins))
  931. (define (network-manager-accounts config)
  932. "Return the list of <user-account> and <user-group> for CONFIG."
  933. (define nologin
  934. (file-append shadow "/sbin/nologin"))
  935. (define accounts
  936. (append-map (lambda (package)
  937. (map (lambda (name)
  938. (user-account (system? #t)
  939. (name name)
  940. (group "network-manager")
  941. (comment "NetworkManager helper")
  942. (home-directory "/var/empty")
  943. (create-home-directory? #f)
  944. (shell nologin)))
  945. (or (assoc-ref (package-properties package)
  946. 'user-accounts)
  947. '())))
  948. (network-manager-configuration-vpn-plugins config)))
  949. (match accounts
  950. (()
  951. '())
  952. (_
  953. (cons (user-group (name "network-manager") (system? #t))
  954. accounts))))
  955. (define network-manager-environment
  956. (match-lambda
  957. (($ <network-manager-configuration> network-manager dns vpn-plugins)
  958. ;; Define this variable in the global environment such that
  959. ;; "nmcli connection import type openvpn file foo.ovpn" works.
  960. `(("NM_VPN_PLUGIN_DIR"
  961. . ,(file-append (vpn-plugin-directory vpn-plugins)
  962. "/lib/NetworkManager/VPN"))))))
  963. (define network-manager-shepherd-service
  964. (match-lambda
  965. (($ <network-manager-configuration> network-manager dns vpn-plugins)
  966. (let ((conf (plain-file "NetworkManager.conf"
  967. (string-append "[main]\ndns=" dns "\n")))
  968. (vpn (vpn-plugin-directory vpn-plugins)))
  969. (list (shepherd-service
  970. (documentation "Run the NetworkManager.")
  971. (provision '(networking))
  972. (requirement '(user-processes dbus-system wpa-supplicant loopback))
  973. (start #~(make-forkexec-constructor
  974. (list (string-append #$network-manager
  975. "/sbin/NetworkManager")
  976. (string-append "--config=" #$conf)
  977. "--no-daemon")
  978. #:environment-variables
  979. (list (string-append "NM_VPN_PLUGIN_DIR=" #$vpn
  980. "/lib/NetworkManager/VPN")
  981. ;; Override non-existent default users
  982. "NM_OPENVPN_USER="
  983. "NM_OPENVPN_GROUP=")))
  984. (stop #~(make-kill-destructor))))))))
  985. (define network-manager-service-type
  986. (let
  987. ((config->packages
  988. (match-lambda
  989. (($ <network-manager-configuration> network-manager _ vpn-plugins)
  990. `(,network-manager ,@vpn-plugins)))))
  991. (service-type
  992. (name 'network-manager)
  993. (extensions
  994. (list (service-extension shepherd-root-service-type
  995. network-manager-shepherd-service)
  996. (service-extension dbus-root-service-type config->packages)
  997. (service-extension polkit-service-type
  998. (compose
  999. list
  1000. network-manager-configuration-network-manager))
  1001. (service-extension account-service-type
  1002. network-manager-accounts)
  1003. (service-extension activation-service-type
  1004. network-manager-activation)
  1005. (service-extension session-environment-service-type
  1006. network-manager-environment)
  1007. ;; Add network-manager to the system profile.
  1008. (service-extension profile-service-type config->packages)))
  1009. (default-value (network-manager-configuration))
  1010. (description
  1011. "Run @uref{https://wiki.gnome.org/Projects/NetworkManager,
  1012. NetworkManager}, a network management daemon that aims to simplify wired and
  1013. wireless networking."))))
  1014. ;;;
  1015. ;;; Connman
  1016. ;;;
  1017. (define-record-type* <connman-configuration>
  1018. connman-configuration make-connman-configuration
  1019. connman-configuration?
  1020. (connman connman-configuration-connman
  1021. (default connman))
  1022. (disable-vpn? connman-configuration-disable-vpn?
  1023. (default #f)))
  1024. (define (connman-activation config)
  1025. (let ((disable-vpn? (connman-configuration-disable-vpn? config)))
  1026. (with-imported-modules '((guix build utils))
  1027. #~(begin
  1028. (use-modules (guix build utils))
  1029. (mkdir-p "/var/lib/connman/")
  1030. (unless #$disable-vpn?
  1031. (mkdir-p "/var/lib/connman-vpn/"))))))
  1032. (define (connman-shepherd-service config)
  1033. "Return a shepherd service for Connman"
  1034. (and
  1035. (connman-configuration? config)
  1036. (let ((connman (connman-configuration-connman config))
  1037. (disable-vpn? (connman-configuration-disable-vpn? config)))
  1038. (list (shepherd-service
  1039. (documentation "Run Connman")
  1040. (provision '(networking))
  1041. (requirement
  1042. '(user-processes dbus-system loopback wpa-supplicant))
  1043. (start #~(make-forkexec-constructor
  1044. (list (string-append #$connman
  1045. "/sbin/connmand")
  1046. "--nodaemon"
  1047. "--nodnsproxy"
  1048. #$@(if disable-vpn? '("--noplugin=vpn") '()))
  1049. ;; As connman(8) notes, when passing '-n', connman
  1050. ;; "directs log output to the controlling terminal in
  1051. ;; addition to syslog." Redirect stdout and stderr
  1052. ;; to avoid spamming the console (XXX: for some reason
  1053. ;; redirecting to /dev/null doesn't work.)
  1054. #:log-file "/var/log/connman.log"))
  1055. (stop #~(make-kill-destructor)))))))
  1056. (define connman-service-type
  1057. (let ((connman-package (compose list connman-configuration-connman)))
  1058. (service-type (name 'connman)
  1059. (extensions
  1060. (list (service-extension shepherd-root-service-type
  1061. connman-shepherd-service)
  1062. (service-extension polkit-service-type
  1063. connman-package)
  1064. (service-extension dbus-root-service-type
  1065. connman-package)
  1066. (service-extension activation-service-type
  1067. connman-activation)
  1068. ;; Add connman to the system profile.
  1069. (service-extension profile-service-type
  1070. connman-package)))
  1071. (default-value (connman-configuration))
  1072. (description
  1073. "Run @url{https://01.org/connman,Connman},
  1074. a network connection manager."))))
  1075. ;;;
  1076. ;;; Modem manager
  1077. ;;;
  1078. (define modem-manager-service-type
  1079. (let ((config->package
  1080. (match-lambda
  1081. (($ <modem-manager-configuration> modem-manager)
  1082. (list modem-manager)))))
  1083. (service-type (name 'modem-manager)
  1084. (extensions
  1085. (list (service-extension dbus-root-service-type
  1086. config->package)
  1087. (service-extension udev-service-type
  1088. config->package)
  1089. (service-extension polkit-service-type
  1090. config->package)))
  1091. (default-value (modem-manager-configuration))
  1092. (description
  1093. "Run @uref{https://wiki.gnome.org/Projects/ModemManager,
  1094. ModemManager}, a modem management daemon that aims to simplify dialup
  1095. networking."))))
  1096. ;;;
  1097. ;;; USB_ModeSwitch
  1098. ;;;
  1099. (define-record-type* <usb-modeswitch-configuration>
  1100. usb-modeswitch-configuration make-usb-modeswitch-configuration
  1101. usb-modeswitch-configuration?
  1102. (usb-modeswitch usb-modeswitch-configuration-usb-modeswitch
  1103. (default usb-modeswitch))
  1104. (usb-modeswitch-data usb-modeswitch-configuration-usb-modeswitch-data
  1105. (default usb-modeswitch-data))
  1106. (config-file usb-modeswitch-configuration-config-file
  1107. (default #~(string-append #$usb-modeswitch:dispatcher
  1108. "/etc/usb_modeswitch.conf"))))
  1109. (define (usb-modeswitch-sh usb-modeswitch config-file)
  1110. "Build a copy of usb_modeswitch.sh located in package USB-MODESWITCH,
  1111. modified to pass the CONFIG-FILE in its calls to usb_modeswitch_dispatcher,
  1112. and wrap it to actually find the dispatcher in USB-MODESWITCH. The script
  1113. will be run by USB_ModeSwitch’s udev rules file when a modeswitchable USB
  1114. device is detected."
  1115. (computed-file
  1116. "usb_modeswitch-sh"
  1117. (with-imported-modules '((guix build utils))
  1118. #~(begin
  1119. (use-modules (guix build utils))
  1120. (let ((cfg-param
  1121. #$(if config-file
  1122. #~(string-append " --config-file=" #$config-file)
  1123. "")))
  1124. (mkdir #$output)
  1125. (install-file (string-append #$usb-modeswitch:dispatcher
  1126. "/lib/udev/usb_modeswitch")
  1127. #$output)
  1128. ;; insert CFG-PARAM into usb_modeswitch_dispatcher command-lines
  1129. (substitute* (string-append #$output "/usb_modeswitch")
  1130. (("(exec usb_modeswitch_dispatcher .*)( 2>>)" _ left right)
  1131. (string-append left cfg-param right))
  1132. (("(exec usb_modeswitch_dispatcher .*)( &)" _ left right)
  1133. (string-append left cfg-param right)))
  1134. ;; wrap-program needs bash in PATH:
  1135. (putenv (string-append "PATH=" #$bash "/bin"))
  1136. (wrap-program (string-append #$output "/usb_modeswitch")
  1137. `("PATH" ":" = (,(string-append #$coreutils "/bin")
  1138. ,(string-append
  1139. #$usb-modeswitch:dispatcher
  1140. "/bin")))))))))
  1141. (define (usb-modeswitch-configuration->udev-rules config)
  1142. "Build a rules file for extending udev-service-type from the rules in the
  1143. usb-modeswitch package specified in CONFIG. The rules file will invoke
  1144. usb_modeswitch.sh from the usb-modeswitch package, modified to pass the right
  1145. config file."
  1146. (match config
  1147. (($ <usb-modeswitch-configuration> usb-modeswitch data config-file)
  1148. (computed-file
  1149. "usb_modeswitch.rules"
  1150. (with-imported-modules '((guix build utils))
  1151. #~(begin
  1152. (use-modules (guix build utils))
  1153. (let ((in (string-append #$data "/udev/40-usb_modeswitch.rules"))
  1154. (out (string-append #$output "/lib/udev/rules.d"))
  1155. (script #$(usb-modeswitch-sh usb-modeswitch config-file)))
  1156. (mkdir-p out)
  1157. (chdir out)
  1158. (install-file in out)
  1159. (substitute* "40-usb_modeswitch.rules"
  1160. (("PROGRAM=\"usb_modeswitch")
  1161. (string-append "PROGRAM=\"" script "/usb_modeswitch"))
  1162. (("RUN\\+=\"usb_modeswitch")
  1163. (string-append "RUN+=\"" script "/usb_modeswitch"))))))))))
  1164. (define usb-modeswitch-service-type
  1165. (service-type
  1166. (name 'usb-modeswitch)
  1167. (extensions
  1168. (list
  1169. (service-extension
  1170. udev-service-type
  1171. (lambda (config)
  1172. (let ((rules (usb-modeswitch-configuration->udev-rules config)))
  1173. (list rules))))))
  1174. (default-value (usb-modeswitch-configuration))
  1175. (description "Run @uref{http://www.draisberghof.de/usb_modeswitch/,
  1176. USB_ModeSwitch}, a mode switching tool for controlling USB devices with
  1177. multiple @dfn{modes}. When plugged in for the first time many USB
  1178. devices (primarily high-speed WAN modems) act like a flash storage containing
  1179. installers for Windows drivers. USB_ModeSwitch replays the sequence the
  1180. Windows drivers would send to switch their mode from storage to modem (or
  1181. whatever the thing is supposed to do).")))
  1182. ;;;
  1183. ;;; WPA supplicant
  1184. ;;;
  1185. (define-record-type* <wpa-supplicant-configuration>
  1186. wpa-supplicant-configuration make-wpa-supplicant-configuration
  1187. wpa-supplicant-configuration?
  1188. (wpa-supplicant wpa-supplicant-configuration-wpa-supplicant ;<package>
  1189. (default wpa-supplicant))
  1190. (requirement wpa-supplicant-configuration-requirement ;list of symbols
  1191. (default '(user-processes loopback syslogd)))
  1192. (pid-file wpa-supplicant-configuration-pid-file ;string
  1193. (default "/var/run/wpa_supplicant.pid"))
  1194. (dbus? wpa-supplicant-configuration-dbus? ;Boolean
  1195. (default #t))
  1196. (interface wpa-supplicant-configuration-interface ;#f | string
  1197. (default #f))
  1198. (config-file wpa-supplicant-configuration-config-file ;#f | <file-like>
  1199. (default #f))
  1200. (extra-options wpa-supplicant-configuration-extra-options ;list of strings
  1201. (default '())))
  1202. (define wpa-supplicant-shepherd-service
  1203. (match-lambda
  1204. (($ <wpa-supplicant-configuration> wpa-supplicant requirement pid-file dbus?
  1205. interface config-file extra-options)
  1206. (list (shepherd-service
  1207. (documentation "Run the WPA supplicant daemon")
  1208. (provision '(wpa-supplicant))
  1209. (requirement (if dbus?
  1210. (cons 'dbus-system requirement)
  1211. requirement))
  1212. (start #~(make-forkexec-constructor
  1213. (list (string-append #$wpa-supplicant
  1214. "/sbin/wpa_supplicant")
  1215. (string-append "-P" #$pid-file)
  1216. "-B" ;run in background
  1217. "-s" ;log to syslogd
  1218. #$@(if dbus?
  1219. #~("-u")
  1220. #~())
  1221. #$@(if interface
  1222. #~((string-append "-i" #$interface))
  1223. #~())
  1224. #$@(if config-file
  1225. #~((string-append "-c" #$config-file))
  1226. #~())
  1227. #$@extra-options)
  1228. #:pid-file #$pid-file))
  1229. (stop #~(make-kill-destructor)))))))
  1230. (define wpa-supplicant-service-type
  1231. (let ((config->package
  1232. (match-lambda
  1233. (($ <wpa-supplicant-configuration> wpa-supplicant)
  1234. (list wpa-supplicant)))))
  1235. (service-type (name 'wpa-supplicant)
  1236. (extensions
  1237. (list (service-extension shepherd-root-service-type
  1238. wpa-supplicant-shepherd-service)
  1239. (service-extension dbus-root-service-type config->package)
  1240. (service-extension profile-service-type config->package)))
  1241. (description "Run the WPA Supplicant daemon, a service that
  1242. implements authentication, key negotiation and more for wireless networks.")
  1243. (default-value (wpa-supplicant-configuration)))))
  1244. ;;;
  1245. ;;; Hostapd.
  1246. ;;;
  1247. (define-record-type* <hostapd-configuration>
  1248. hostapd-configuration make-hostapd-configuration
  1249. hostapd-configuration?
  1250. (package hostapd-configuration-package
  1251. (default hostapd))
  1252. (interface hostapd-configuration-interface ;string
  1253. (default "wlan0"))
  1254. (ssid hostapd-configuration-ssid) ;string
  1255. (broadcast-ssid? hostapd-configuration-broadcast-ssid? ;Boolean
  1256. (default #t))
  1257. (channel hostapd-configuration-channel ;integer
  1258. (default 1))
  1259. (driver hostapd-configuration-driver ;string
  1260. (default "nl80211"))
  1261. ;; See <https://w1.fi/cgit/hostap/plain/hostapd/hostapd.conf> for a list of
  1262. ;; additional options we could add.
  1263. (extra-settings hostapd-configuration-extra-settings ;string
  1264. (default "")))
  1265. (define (hostapd-configuration-file config)
  1266. "Return the configuration file for CONFIG, a <hostapd-configuration>."
  1267. (match-record config <hostapd-configuration>
  1268. (interface ssid broadcast-ssid? channel driver extra-settings)
  1269. (plain-file "hostapd.conf"
  1270. (string-append "\
  1271. # Generated from your Guix configuration.
  1272. interface=" interface "
  1273. ssid=" ssid "
  1274. ignore_broadcast_ssid=" (if broadcast-ssid? "0" "1") "
  1275. channel=" (number->string channel) "\n"
  1276. extra-settings "\n"))))
  1277. (define* (hostapd-shepherd-services config #:key (requirement '()))
  1278. "Return Shepherd services for hostapd."
  1279. (list (shepherd-service
  1280. (provision '(hostapd))
  1281. (requirement `(user-processes ,@requirement))
  1282. (documentation "Run the hostapd WiFi access point daemon.")
  1283. (start #~(make-forkexec-constructor
  1284. (list #$(file-append hostapd "/sbin/hostapd")
  1285. #$(hostapd-configuration-file config))
  1286. #:log-file "/var/log/hostapd.log"))
  1287. (stop #~(make-kill-destructor)))))
  1288. (define hostapd-service-type
  1289. (service-type
  1290. (name 'hostapd)
  1291. (extensions
  1292. (list (service-extension shepherd-root-service-type
  1293. hostapd-shepherd-services)))
  1294. (description
  1295. "Run the @uref{https://w1.fi/hostapd/, hostapd} daemon for Wi-Fi access
  1296. points and authentication servers.")))
  1297. (define (simulated-wifi-shepherd-services config)
  1298. "Return Shepherd services to run hostapd with CONFIG, a
  1299. <hostapd-configuration>, as well as services to set up WiFi hardware
  1300. simulation."
  1301. (append (hostapd-shepherd-services config
  1302. #:requirement
  1303. '(unblocked-wifi
  1304. kernel-module-loader))
  1305. (list (shepherd-service
  1306. (provision '(unblocked-wifi))
  1307. (requirement '(file-systems kernel-module-loader))
  1308. (documentation
  1309. "Unblock WiFi devices for use by mac80211_hwsim.")
  1310. (start #~(lambda _
  1311. (invoke #$(file-append util-linux "/sbin/rfkill")
  1312. "unblock" "0")
  1313. (invoke #$(file-append util-linux "/sbin/rfkill")
  1314. "unblock" "1")))
  1315. (one-shot? #t)))))
  1316. (define simulated-wifi-service-type
  1317. (service-type
  1318. (name 'simulated-wifi)
  1319. (extensions
  1320. (list (service-extension shepherd-root-service-type
  1321. simulated-wifi-shepherd-services)
  1322. (service-extension kernel-module-loader-service-type
  1323. (const '("mac80211_hwsim")))))
  1324. (default-value (hostapd-configuration
  1325. (interface "wlan1")
  1326. (ssid "Test Network")))
  1327. (description "Run hostapd to simulate WiFi connectivity.")))
  1328. ;;;
  1329. ;;; Open vSwitch
  1330. ;;;
  1331. (define-record-type* <openvswitch-configuration>
  1332. openvswitch-configuration make-openvswitch-configuration
  1333. openvswitch-configuration?
  1334. (package openvswitch-configuration-package
  1335. (default openvswitch)))
  1336. (define openvswitch-activation
  1337. (match-lambda
  1338. (($ <openvswitch-configuration> package)
  1339. (let ((ovsdb-tool (file-append package "/bin/ovsdb-tool")))
  1340. (with-imported-modules '((guix build utils))
  1341. #~(begin
  1342. (use-modules (guix build utils))
  1343. (mkdir-p "/var/run/openvswitch")
  1344. (mkdir-p "/var/lib/openvswitch")
  1345. (let ((conf.db "/var/lib/openvswitch/conf.db"))
  1346. (unless (file-exists? conf.db)
  1347. (system* #$ovsdb-tool "create" conf.db)))))))))
  1348. (define openvswitch-shepherd-service
  1349. (match-lambda
  1350. (($ <openvswitch-configuration> package)
  1351. (let ((ovsdb-server (file-append package "/sbin/ovsdb-server"))
  1352. (ovs-vswitchd (file-append package "/sbin/ovs-vswitchd")))
  1353. (list
  1354. (shepherd-service
  1355. (provision '(ovsdb))
  1356. (documentation "Run the Open vSwitch database server.")
  1357. (start #~(make-forkexec-constructor
  1358. (list #$ovsdb-server "--pidfile"
  1359. "--remote=punix:/var/run/openvswitch/db.sock")
  1360. #:pid-file "/var/run/openvswitch/ovsdb-server.pid"))
  1361. (stop #~(make-kill-destructor)))
  1362. (shepherd-service
  1363. (provision '(vswitchd))
  1364. (requirement '(ovsdb))
  1365. (documentation "Run the Open vSwitch daemon.")
  1366. (start #~(make-forkexec-constructor
  1367. (list #$ovs-vswitchd "--pidfile")
  1368. #:pid-file "/var/run/openvswitch/ovs-vswitchd.pid"))
  1369. (stop #~(make-kill-destructor))))))))
  1370. (define openvswitch-service-type
  1371. (service-type
  1372. (name 'openvswitch)
  1373. (extensions
  1374. (list (service-extension activation-service-type
  1375. openvswitch-activation)
  1376. (service-extension profile-service-type
  1377. (compose list openvswitch-configuration-package))
  1378. (service-extension shepherd-root-service-type
  1379. openvswitch-shepherd-service)))
  1380. (description
  1381. "Run @uref{http://www.openvswitch.org, Open vSwitch}, a multilayer virtual
  1382. switch designed to enable massive network automation through programmatic
  1383. extension.")
  1384. (default-value (openvswitch-configuration))))
  1385. ;;;
  1386. ;;; iptables
  1387. ;;;
  1388. (define %iptables-accept-all-rules
  1389. (plain-file "iptables-accept-all.rules"
  1390. "*filter
  1391. :INPUT ACCEPT
  1392. :FORWARD ACCEPT
  1393. :OUTPUT ACCEPT
  1394. COMMIT
  1395. "))
  1396. (define-record-type* <iptables-configuration>
  1397. iptables-configuration make-iptables-configuration iptables-configuration?
  1398. (iptables iptables-configuration-iptables
  1399. (default iptables))
  1400. (ipv4-rules iptables-configuration-ipv4-rules
  1401. (default %iptables-accept-all-rules))
  1402. (ipv6-rules iptables-configuration-ipv6-rules
  1403. (default %iptables-accept-all-rules)))
  1404. (define iptables-shepherd-service
  1405. (match-lambda
  1406. (($ <iptables-configuration> iptables ipv4-rules ipv6-rules)
  1407. (let ((iptables-restore (file-append iptables "/sbin/iptables-restore"))
  1408. (ip6tables-restore (file-append iptables "/sbin/ip6tables-restore")))
  1409. (shepherd-service
  1410. (documentation "Packet filtering framework")
  1411. (provision '(iptables))
  1412. (start #~(lambda _
  1413. (invoke #$iptables-restore #$ipv4-rules)
  1414. (invoke #$ip6tables-restore #$ipv6-rules)))
  1415. (stop #~(lambda _
  1416. (invoke #$iptables-restore #$%iptables-accept-all-rules)
  1417. (invoke #$ip6tables-restore #$%iptables-accept-all-rules))))))))
  1418. (define iptables-service-type
  1419. (service-type
  1420. (name 'iptables)
  1421. (description
  1422. "Run @command{iptables-restore}, setting up the specified rules.")
  1423. (extensions
  1424. (list (service-extension shepherd-root-service-type
  1425. (compose list iptables-shepherd-service))))))
  1426. ;;;
  1427. ;;; nftables
  1428. ;;;
  1429. (define %default-nftables-ruleset
  1430. (plain-file "nftables.conf"
  1431. "# A simple and safe firewall
  1432. table inet filter {
  1433. chain input {
  1434. type filter hook input priority 0; policy drop;
  1435. # early drop of invalid connections
  1436. ct state invalid drop
  1437. # allow established/related connections
  1438. ct state { established, related } accept
  1439. # allow from loopback
  1440. iifname lo accept
  1441. # allow icmp
  1442. ip protocol icmp accept
  1443. ip6 nexthdr icmpv6 accept
  1444. # allow ssh
  1445. tcp dport ssh accept
  1446. # reject everything else
  1447. reject with icmpx type port-unreachable
  1448. }
  1449. chain forward {
  1450. type filter hook forward priority 0; policy drop;
  1451. }
  1452. chain output {
  1453. type filter hook output priority 0; policy accept;
  1454. }
  1455. }
  1456. "))
  1457. (define-record-type* <nftables-configuration>
  1458. nftables-configuration
  1459. make-nftables-configuration
  1460. nftables-configuration?
  1461. (package nftables-configuration-package
  1462. (default nftables))
  1463. (ruleset nftables-configuration-ruleset ; file-like object
  1464. (default %default-nftables-ruleset)))
  1465. (define nftables-shepherd-service
  1466. (match-lambda
  1467. (($ <nftables-configuration> package ruleset)
  1468. (let ((nft (file-append package "/sbin/nft")))
  1469. (shepherd-service
  1470. (documentation "Packet filtering and classification")
  1471. (provision '(nftables))
  1472. (start #~(lambda _
  1473. (invoke #$nft "--file" #$ruleset)))
  1474. (stop #~(lambda _
  1475. (invoke #$nft "flush" "ruleset"))))))))
  1476. (define nftables-service-type
  1477. (service-type
  1478. (name 'nftables)
  1479. (description
  1480. "Run @command{nft}, setting up the specified ruleset.")
  1481. (extensions
  1482. (list (service-extension shepherd-root-service-type
  1483. (compose list nftables-shepherd-service))
  1484. (service-extension profile-service-type
  1485. (compose list nftables-configuration-package))))
  1486. (default-value (nftables-configuration))))
  1487. ;;;
  1488. ;;; PageKite
  1489. ;;;
  1490. (define-record-type* <pagekite-configuration>
  1491. pagekite-configuration
  1492. make-pagekite-configuration
  1493. pagekite-configuration?
  1494. (package pagekite-configuration-package
  1495. (default pagekite))
  1496. (kitename pagekite-configuration-kitename
  1497. (default #f))
  1498. (kitesecret pagekite-configuration-kitesecret
  1499. (default #f))
  1500. (frontend pagekite-configuration-frontend
  1501. (default #f))
  1502. (kites pagekite-configuration-kites
  1503. (default '("http:@kitename:localhost:80:@kitesecret")))
  1504. (extra-file pagekite-configuration-extra-file
  1505. (default #f)))
  1506. (define (pagekite-configuration-file config)
  1507. (match-record config <pagekite-configuration>
  1508. (package kitename kitesecret frontend kites extra-file)
  1509. (mixed-text-file "pagekite.rc"
  1510. (if extra-file
  1511. (string-append "optfile = " extra-file "\n")
  1512. "")
  1513. (if kitename
  1514. (string-append "kitename = " kitename "\n")
  1515. "")
  1516. (if kitesecret
  1517. (string-append "kitesecret = " kitesecret "\n")
  1518. "")
  1519. (if frontend
  1520. (string-append "frontend = " frontend "\n")
  1521. "defaults\n")
  1522. (string-join (map (lambda (kite)
  1523. (string-append "service_on = " kite))
  1524. kites)
  1525. "\n"
  1526. 'suffix))))
  1527. (define (pagekite-shepherd-service config)
  1528. (match-record config <pagekite-configuration>
  1529. (package kitename kitesecret frontend kites extra-file)
  1530. (with-imported-modules (source-module-closure
  1531. '((gnu build shepherd)
  1532. (gnu system file-systems)))
  1533. (shepherd-service
  1534. (documentation "Run the PageKite service.")
  1535. (provision '(pagekite))
  1536. (requirement '(networking))
  1537. (modules '((gnu build shepherd)
  1538. (gnu system file-systems)))
  1539. (start #~(make-forkexec-constructor/container
  1540. (list #$(file-append package "/bin/pagekite")
  1541. "--clean"
  1542. "--nullui"
  1543. "--nocrashreport"
  1544. "--runas=pagekite:pagekite"
  1545. (string-append "--optfile="
  1546. #$(pagekite-configuration-file config)))
  1547. #:log-file "/var/log/pagekite.log"
  1548. #:mappings #$(if extra-file
  1549. #~(list (file-system-mapping
  1550. (source #$extra-file)
  1551. (target source)))
  1552. #~'())))
  1553. ;; SIGTERM doesn't always work for some reason.
  1554. (stop #~(make-kill-destructor SIGINT))))))
  1555. (define %pagekite-accounts
  1556. (list (user-group (name "pagekite") (system? #t))
  1557. (user-account
  1558. (name "pagekite")
  1559. (group "pagekite")
  1560. (system? #t)
  1561. (comment "PageKite user")
  1562. (home-directory "/var/empty")
  1563. (shell (file-append shadow "/sbin/nologin")))))
  1564. (define pagekite-service-type
  1565. (service-type
  1566. (name 'pagekite)
  1567. (default-value (pagekite-configuration))
  1568. (extensions
  1569. (list (service-extension shepherd-root-service-type
  1570. (compose list pagekite-shepherd-service))
  1571. (service-extension account-service-type
  1572. (const %pagekite-accounts))))
  1573. (description
  1574. "Run @url{https://pagekite.net/,PageKite}, a tunneling solution to make
  1575. local servers publicly accessible on the web, even behind NATs and firewalls.")))
  1576. ;;;
  1577. ;;; Yggdrasil
  1578. ;;;
  1579. (define-record-type* <yggdrasil-configuration>
  1580. yggdrasil-configuration
  1581. make-yggdrasil-configuration
  1582. yggdrasil-configuration?
  1583. (package yggdrasil-configuration-package
  1584. (default yggdrasil))
  1585. (json-config yggdrasil-configuration-json-config
  1586. (default '()))
  1587. (config-file yggdrasil-config-file
  1588. (default "/etc/yggdrasil-private.conf"))
  1589. (autoconf? yggdrasil-configuration-autoconf?
  1590. (default #f))
  1591. (log-level yggdrasil-configuration-log-level
  1592. (default 'info))
  1593. (log-to yggdrasil-configuration-log-to
  1594. (default 'stdout)))
  1595. (define (yggdrasil-configuration-file config)
  1596. (define (scm->yggdrasil-json x)
  1597. (define key-value?
  1598. dotted-list?)
  1599. (define (param->camel str)
  1600. (string-concatenate
  1601. (map
  1602. string-capitalize
  1603. (string-split str (cut eqv? <> #\-)))))
  1604. (cond
  1605. ((key-value? x)
  1606. (let ((k (car x))
  1607. (v (cdr x)))
  1608. (cons
  1609. (if (symbol? k)
  1610. (param->camel (symbol->string k))
  1611. k)
  1612. v)))
  1613. ((list? x) (map scm->yggdrasil-json x))
  1614. ((vector? x) (vector-map scm->yggdrasil-json x))
  1615. (else x)))
  1616. (computed-file
  1617. "yggdrasil.conf"
  1618. #~(call-with-output-file #$output
  1619. (lambda (port)
  1620. ;; it's HJSON, so comments are a-okay
  1621. (display "# Generated by yggdrasil-service\n" port)
  1622. (display #$(scm->json-string
  1623. (scm->yggdrasil-json
  1624. (yggdrasil-configuration-json-config config)))
  1625. port)))))
  1626. (define (yggdrasil-shepherd-service config)
  1627. "Return a <shepherd-service> for yggdrasil with CONFIG."
  1628. (define yggdrasil-command
  1629. #~(append
  1630. (list (string-append
  1631. #$(yggdrasil-configuration-package config)
  1632. "/bin/yggdrasil")
  1633. "-useconffile"
  1634. #$(yggdrasil-configuration-file config))
  1635. (if #$(yggdrasil-configuration-autoconf? config)
  1636. '("-autoconf")
  1637. '())
  1638. (let ((extraconf #$(yggdrasil-config-file config)))
  1639. (if extraconf
  1640. (list "-extraconffile" extraconf)
  1641. '()))
  1642. (list "-loglevel"
  1643. #$(symbol->string
  1644. (yggdrasil-configuration-log-level config))
  1645. "-logto"
  1646. #$(symbol->string
  1647. (yggdrasil-configuration-log-to config)))))
  1648. (list (shepherd-service
  1649. (documentation "Connect to the Yggdrasil mesh network")
  1650. (provision '(yggdrasil))
  1651. (requirement '(networking))
  1652. (start #~(make-forkexec-constructor
  1653. #$yggdrasil-command
  1654. #:log-file "/var/log/yggdrasil.log"
  1655. #:group "yggdrasil"))
  1656. (stop #~(make-kill-destructor)))))
  1657. (define %yggdrasil-accounts
  1658. (list (user-group (name "yggdrasil") (system? #t))))
  1659. (define yggdrasil-service-type
  1660. (service-type
  1661. (name 'yggdrasil)
  1662. (description
  1663. "Connect to the Yggdrasil mesh network.
  1664. See yggdrasil -genconf for config options.")
  1665. (extensions
  1666. (list (service-extension shepherd-root-service-type
  1667. yggdrasil-shepherd-service)
  1668. (service-extension account-service-type
  1669. (const %yggdrasil-accounts))
  1670. (service-extension profile-service-type
  1671. (compose list yggdrasil-configuration-package))))))
  1672. ;;;
  1673. ;;; Keepalived
  1674. ;;;
  1675. (define-record-type* <keepalived-configuration>
  1676. keepalived-configuration make-keepalived-configuration
  1677. keepalived-configuration?
  1678. (keepalived keepalived-configuration-keepalived ;<package>
  1679. (default keepalived))
  1680. (config-file keepalived-configuration-config-file ;file-like
  1681. (default #f)))
  1682. (define keepalived-shepherd-service
  1683. (match-lambda
  1684. (($ <keepalived-configuration> keepalived config-file)
  1685. (list
  1686. (shepherd-service
  1687. (provision '(keepalived))
  1688. (documentation "Run keepalived.")
  1689. (requirement '(loopback))
  1690. (start #~(make-forkexec-constructor
  1691. (list (string-append #$keepalived "/sbin/keepalived")
  1692. "--dont-fork" "--log-console" "--log-detail"
  1693. "--pid=/var/run/keepalived.pid"
  1694. (string-append "--use-file=" #$config-file))
  1695. #:pid-file "/var/run/keepalived.pid"
  1696. #:log-file "/var/log/keepalived.log"))
  1697. (respawn? #f)
  1698. (stop #~(make-kill-destructor)))))))
  1699. (define keepalived-service-type
  1700. (service-type (name 'keepalived)
  1701. (extensions (list (service-extension shepherd-root-service-type
  1702. keepalived-shepherd-service)))
  1703. (description
  1704. "Run @uref{https://www.keepalived.org/, Keepalived}
  1705. routing software.")))
  1706. ;;; networking.scm ends here