fork/home-manager
git clone https://git.julia.blue/fork/home-manager
75790441
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . programs . ssh ; 8 9isPath = x :builtins . substring 0 1 ( toString x ) == "/" ; 10 11addressPort = entry : 12if isPath entry . address 13then " ${ entry . address } " 14else " [ ${ entry . address } ]: ${ toString entry . port } " ; 15 16unwords = builtins . concatStringsSep " " ; 17 18mkSetEnvStr = envStr :unwords 19( mapAttrsToList 20( name :value :'' ${ name } =" ${ escape [ "\"" "\\" ] ( toString value )} "'' ) 21envStr 22); 23 24bindOptions = { 25address = mkOption { 26type = types . str ; 27default = "localhost" ; 28example = "example.org" ; 29description = "The address where to bind the port." ; 30}; 31 32port = mkOption { 33type = types . nullOr types . port ; 34default = null ; 35example = 8080 ; 36description = "Specifies port number to bind on bind address." ; 37}; 38}; 39 40dynamicForwardModule = types . submodule { 41options = bindOptions ; 42}; 43 44forwardModule = types . submodule { 45options = { 46bind = bindOptions ; 47 48host = { 49address = mkOption { 50type = types . nullOr types . str ; 51default = null ; 52example = "example.org" ; 53description = "The address where to forward the traffic to." ; 54}; 55 56port = mkOption { 57type = types . nullOr types . port ; 58default = null ; 59example = 80 ; 60description = "Specifies port number to forward the traffic to." ; 61}; 62}; 63}; 64}; 65 66matchBlockModule = types . submodule ({ dagName , ...} :{ 67options = { 68host = mkOption { 69type = types . nullOr types . str ; 70default = null ; 71example = "*.example.org" ; 72description = '' 73`Host` pattern used by this conditional block. 74See 75{manpage}`ssh_config(5)` 76for `Host` block details. 77This option is ignored if 78{option}`ssh.matchBlocks.*.match` 79if defined. 80'' ; 81}; 82 83match = mkOption { 84type = types . nullOr types . str ; 85default = null ; 86example = "host <hostname> canonical\nhost <hostname> exec \"ping -c1 -q 192.168.17.1\"" ; 87description = '' 88`Match` block conditions used by this block. See 89{manpage}`ssh_config(5)` 90for `Match` block details. 91This option takes precedence over 92{option}`ssh.matchBlocks.*.host` 93if defined. 94'' ; 95}; 96 97port = mkOption { 98type = types . nullOr types . port ; 99default = null ; 100description = "Specifies port number to connect on remote host." ; 101}; 102 103forwardAgent = mkOption { 104default = null ; 105type = types . nullOr types . bool ; 106description = '' 107Whether the connection to the authentication agent (if any) 108will be forwarded to the remote machine. 109'' ; 110}; 111 112forwardX11 = mkOption { 113type = types . bool ; 114default = false ; 115description = '' 116Specifies whether X11 connections will be automatically redirected 117over the secure channel and {env}`DISPLAY` set. 118'' ; 119}; 120 121forwardX11Trusted = mkOption { 122type = types . bool ; 123default = false ; 124description = '' 125Specifies whether remote X11 clients will have full access to the 126original X11 display. 127'' ; 128}; 129 130identitiesOnly = mkOption { 131type = types . bool ; 132default = false ; 133description = '' 134Specifies that ssh should only use the authentication 135identity explicitly configured in the 136{file}`~/.ssh/config` files or passed on the 137ssh command-line, even if {command}`ssh-agent` 138offers more identities. 139'' ; 140}; 141 142identityFile = mkOption { 143type = with types ; either ( listOf str ) ( nullOr str ); 144default = []; 145apply = p : 146if p == null then [] 147else if isString p then [ p ] 148else p ; 149description = '' 150Specifies files from which the user identity is read. 151Identities will be tried in the given order. 152'' ; 153}; 154 155user = mkOption { 156type = types . nullOr types . str ; 157default = null ; 158description = "Specifies the user to log in as." ; 159}; 160 161hostname = mkOption { 162type = types . nullOr types . str ; 163default = null ; 164description = "Specifies the real host name to log into." ; 165}; 166 167serverAliveInterval = mkOption { 168type = types . int ; 169default = 0 ; 170description = 171"Set timeout in seconds after which response will be requested." ; 172}; 173 174serverAliveCountMax = mkOption { 175type = types . ints . positive ; 176default = 3 ; 177description = '' 178Sets the number of server alive messages which may be sent 179without SSH receiving any messages back from the server. 180'' ; 181}; 182 183sendEnv = mkOption { 184type = types . listOf types . str ; 185default = []; 186description = '' 187Environment variables to send from the local host to the 188server. 189'' ; 190}; 191 192setEnv = mkOption { 193type = with types ; attrsOf ( oneOf [ str path int float ]); 194default = {}; 195description = '' 196Environment variables and their value to send to the server. 197'' ; 198}; 199 200compression = mkOption { 201type = types . nullOr types . bool ; 202default = null ; 203description = '' 204Specifies whether to use compression. Omitted from the host 205block when `null`. 206'' ; 207}; 208 209checkHostIP = mkOption { 210type = types . bool ; 211default = true ; 212description = '' 213Check the host IP address in the 214{file}`known_hosts` file. 215'' ; 216}; 217 218proxyCommand = mkOption { 219type = types . nullOr types . str ; 220default = null ; 221description = "The command to use to connect to the server." ; 222}; 223 224proxyJump = mkOption { 225type = types . nullOr types . str ; 226default = null ; 227description = "The proxy host to use to connect to the server." ; 228}; 229 230certificateFile = mkOption { 231type = with types ; either ( listOf str ) ( nullOr str ); 232default = []; 233apply = p : 234if p == null then [] 235else if isString p then [ p ] 236else p ; 237description = '' 238Specifies files from which the user certificate is read. 239'' ; 240}; 241 242addressFamily = mkOption { 243default = null ; 244type = types . nullOr ( types . enum [ "any" "inet" "inet6" ]); 245description = '' 246Specifies which address family to use when connecting. 247'' ; 248}; 249 250localForwards = mkOption { 251type = types . listOf forwardModule ; 252default = []; 253example = literalExpression '' 254[ 255{ 256bind.port = 8080; 257host.address = "10.0.0.13"; 258host.port = 80; 259} 260]; 261'' ; 262description = '' 263Specify local port forwardings. See 264{manpage}`ssh_config(5)` for `LocalForward`. 265'' ; 266}; 267 268remoteForwards = mkOption { 269type = types . listOf forwardModule ; 270default = []; 271example = literalExpression '' 272[ 273{ 274bind.port = 8080; 275host.address = "10.0.0.13"; 276host.port = 80; 277} 278]; 279'' ; 280description = '' 281Specify remote port forwardings. See 282{manpage}`ssh_config(5)` for `RemoteForward`. 283'' ; 284}; 285 286dynamicForwards = mkOption { 287type = types . listOf dynamicForwardModule ; 288default = []; 289example = literalExpression '' 290[ { port = 8080; } ]; 291'' ; 292description = '' 293Specify dynamic port forwardings. See 294{manpage}`ssh_config(5)` for `DynamicForward`. 295'' ; 296}; 297 298extraOptions = mkOption { 299type = types . attrsOf types . str ; 300default = {}; 301description = "Extra configuration options for the host." ; 302}; 303}; 304 305# config.host = mkDefault dagName; 306}); 307 308matchBlockStr = key :cf :concatStringsSep "\n" ( 309let 310hostOrDagName = if cf . host != null then cf . host else key ; 311matchHead = if cf . match != null 312then "Match ${ cf . match } " 313else "Host ${ hostOrDagName } " ; 314in [ " ${ matchHead } " ] 315++ optional ( cf . port != null ) " Port ${ toString cf . port } " 316++ optional ( cf . forwardAgent != null ) " ForwardAgent ${ lib . hm . booleans . yesNo cf . forwardAgent } " 317++ optional cf . forwardX11 " ForwardX11 yes" 318++ optional cf . forwardX11Trusted " ForwardX11Trusted yes" 319++ optional cf . identitiesOnly " IdentitiesOnly yes" 320++ optional ( cf . user != null ) " User ${ cf . user } " 321++ optional ( cf . hostname != null ) " HostName ${ cf . hostname } " 322++ optional ( cf . addressFamily != null ) " AddressFamily ${ cf . addressFamily } " 323++ optional ( cf . sendEnv != []) " SendEnv ${ unwords cf . sendEnv } " 324++ optional ( cf . setEnv != {}) " SetEnv ${ mkSetEnvStr cf . setEnv } " 325++ optional ( cf . serverAliveInterval != 0 ) 326" ServerAliveInterval ${ toString cf . serverAliveInterval } " 327++ optional ( cf . serverAliveCountMax != 3 ) 328" ServerAliveCountMax ${ toString cf . serverAliveCountMax } " 329++ optional ( cf . compression != null ) " Compression ${ lib . hm . booleans . yesNo cf . compression } " 330++ optional ( ! cf . checkHostIP ) " CheckHostIP no" 331++ optional ( cf . proxyCommand != null ) " ProxyCommand ${ cf . proxyCommand } " 332++ optional ( cf . proxyJump != null ) " ProxyJump ${ cf . proxyJump } " 333++ map ( file :" IdentityFile ${ file } " ) cf . identityFile 334++ map ( file :" CertificateFile ${ file } " ) cf . certificateFile 335++ map ( f :" LocalForward" + addressPort f . bind + addressPort f . host ) cf . localForwards 336++ map ( f :" RemoteForward" + addressPort f . bind + addressPort f . host ) cf . remoteForwards 337++ map ( f :" DynamicForward" + addressPort f ) cf . dynamicForwards 338++ mapAttrsToList ( n :v :" ${ n } ${ v } " ) cf . extraOptions 339); 340 341in 342 343{ 344meta . maintainers = [ maintainers . rycee ]; 345 346options . programs . ssh = { 347enable = mkEnableOption "SSH client configuration" ; 348 349package = mkPackageOption pkgs "openssh" { 350nullable = true ; 351default = null ; 352extraDescription = "By default, the client provided by your system is used." ; 353}; 354 355forwardAgent = mkOption { 356default = false ; 357type = types . bool ; 358description = '' 359Whether the connection to the authentication agent (if any) 360will be forwarded to the remote machine. 361'' ; 362}; 363 364compression = mkOption { 365default = false ; 366type = types . bool ; 367description = "Specifies whether to use compression." ; 368}; 369 370serverAliveInterval = mkOption { 371type = types . int ; 372default = 0 ; 373description = '' 374Set default timeout in seconds after which response will be requested. 375'' ; 376}; 377 378serverAliveCountMax = mkOption { 379type = types . ints . positive ; 380default = 3 ; 381description = '' 382Sets the default number of server alive messages which may be 383sent without SSH receiving any messages back from the server. 384'' ; 385}; 386 387hashKnownHosts = mkOption { 388default = false ; 389type = types . bool ; 390description = '' 391Indicates that 392{manpage}`ssh(1)` 393should hash host names and addresses when they are added to 394the known hosts file. 395'' ; 396}; 397 398userKnownHostsFile = mkOption { 399type = types . str ; 400default = "~/.ssh/known_hosts" ; 401description = '' 402Specifies one or more files to use for the user host key 403database, separated by whitespace. The default is 404{file}`~/.ssh/known_hosts`. 405'' ; 406}; 407 408controlMaster = mkOption { 409default = "no" ; 410type = types . enum [ "yes" "no" "ask" "auto" "autoask" ]; 411description = '' 412Configure sharing of multiple sessions over a single network connection. 413'' ; 414}; 415 416controlPath = mkOption { 417type = types . str ; 418default = "~/.ssh/master-%r@%n:%p" ; 419description = '' 420Specify path to the control socket used for connection sharing. 421'' ; 422}; 423 424controlPersist = mkOption { 425type = types . str ; 426default = "no" ; 427example = "10m" ; 428description = '' 429Whether control socket should remain open in the background. 430'' ; 431}; 432 433extraConfig = mkOption { 434type = types . lines ; 435default = "" ; 436description = '' 437Extra configuration. 438'' ; 439}; 440 441extraOptionOverrides = mkOption { 442type = types . attrsOf types . str ; 443default = {}; 444description = '' 445Extra SSH configuration options that take precedence over any 446host specific configuration. 447'' ; 448}; 449 450includes = mkOption { 451type = types . listOf types . str ; 452default = []; 453description = '' 454File globs of ssh config files that should be included via the 455`Include` directive. 456 457See 458{manpage}`ssh_config(5)` 459for more information. 460'' ; 461}; 462 463matchBlocks = mkOption { 464type = hm . types . dagOf matchBlockModule ; 465default = {}; 466example = literalExpression '' 467{ 468"john.example.com" = { 469hostname = "example.com"; 470user = "john"; 471}; 472foo = lib.hm.dag.entryBefore ["john.example.com"] { 473hostname = "example.com"; 474identityFile = "/home/john/.ssh/foo_rsa"; 475}; 476}; 477'' ; 478description = '' 479Specify per-host settings. Note, if the order of rules matter 480then use the DAG functions to express the dependencies as 481shown in the example. 482 483See 484{manpage}`ssh_config(5)` 485for more information. 486'' ; 487}; 488}; 489 490config = mkIf cfg . enable { 491assertions = [ 492{ 493assertion = 494let 495# `builtins.any`/`lib.lists.any` does not return `true` if there are no elements. 496any' = pred :items :if items == [] then true else any pred items ; 497# Check that if `entry.address` is defined, and is a path, that `entry.port` has not 498# been defined. 499noPathWithPort = entry :entry . address != null && isPath entry . address -> entry . port == null ; 500checkDynamic = block :any' noPathWithPort block . dynamicForwards ; 501checkBindAndHost = fwd :noPathWithPort fwd . bind && noPathWithPort fwd . host ; 502checkLocal = block :any' checkBindAndHost block . localForwards ; 503checkRemote = block :any' checkBindAndHost block . remoteForwards ; 504checkMatchBlock = block :all ( fn :fn block ) [ checkLocal checkRemote checkDynamic ]; 505in any' checkMatchBlock ( map ( block :block . data ) ( builtins . attrValues cfg . matchBlocks )); 506message = "Forwarded paths cannot have ports." ; 507} 508]; 509 510home . packages = optional ( cfg . package != null ) cfg . package ; 511 512home . file . ".ssh/config" . text = 513let 514sortedMatchBlocks = hm . dag . topoSort cfg . matchBlocks ; 515sortedMatchBlocksStr = builtins . toJSON sortedMatchBlocks ; 516matchBlocks = 517if sortedMatchBlocks ?result 518then sortedMatchBlocks . result 519else abort "Dependency cycle in SSH match blocks: ${ sortedMatchBlocksStr } " ; 520in '' 521${ concatStringsSep "\n" ( 522( mapAttrsToList ( n : v : " ${ n } ${ v } " ) cfg . extraOptionOverrides ) 523++ ( optional ( cfg . includes != [ ]) '' 524Include ${ concatStringsSep " " cfg . includes } 525'' ) 526++ ( map ( block : matchBlockStr block . name block . data ) matchBlocks ) 527)} 528 529Host * 530ForwardAgent ${ lib . hm . booleans . yesNo cfg . forwardAgent } 531Compression ${ lib . hm . booleans . yesNo cfg . compression } 532ServerAliveInterval ${ toString cfg . serverAliveInterval } 533ServerAliveCountMax ${ toString cfg . serverAliveCountMax } 534HashKnownHosts ${ lib . hm . booleans . yesNo cfg . hashKnownHosts } 535UserKnownHostsFile ${ cfg . userKnownHostsFile } 536ControlMaster ${ cfg . controlMaster } 537ControlPath ${ cfg . controlPath } 538ControlPersist ${ cfg . controlPersist } 539 540${ replaceStrings [ "\n" ] [ "\n " ] cfg . extraConfig } 541'' ; 542 543warnings = mapAttrsToList 544( n :v :"The SSH config match block `programs.ssh.matchBlocks. ${ n } ` sets both of the host and match options.\nThe match option takes precedence." ) 545( filterAttrs ( n :v :v . data . host != null && v . data . match != null ) cfg . matchBlocks ); 546}; 547}