| 
									
										
										
										
											2019-08-30 15:46:57 +02:00
										 |  |  | { config, lib, pkgs, ... }: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | with lib; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let | 
					
						
							|  |  |  |   cfg = config.services.railcar; | 
					
						
							|  |  |  |   generateUnit = name: containerConfig: | 
					
						
							|  |  |  |     let | 
					
						
							|  |  |  |       container = pkgs.ociTools.buildContainer { | 
					
						
							|  |  |  |         args = [ | 
					
						
							|  |  |  |           (pkgs.writeShellScript "run.sh" containerConfig.cmd).outPath | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     in | 
					
						
							|  |  |  |       nameValuePair "railcar-${name}" { | 
					
						
							|  |  |  |         enable = true; | 
					
						
							|  |  |  |         wantedBy = [ "multi-user.target" ]; | 
					
						
							|  |  |  |         serviceConfig = { | 
					
						
							|  |  |  |             ExecStart = ''
 | 
					
						
							|  |  |  |               ${cfg.package}/bin/railcar -r ${cfg.stateDir} run ${name} -b ${container} | 
					
						
							|  |  |  |             '';
 | 
					
						
							|  |  |  |             Type = containerConfig.runType; | 
					
						
							|  |  |  |           }; | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2019-09-03 11:02:52 +02:00
										 |  |  |   mount = with types; (submodule { | 
					
						
							| 
									
										
										
										
											2019-08-30 15:46:57 +02:00
										 |  |  |     options = { | 
					
						
							|  |  |  |       type = mkOption { | 
					
						
							| 
									
										
										
										
											2019-09-06 14:50:59 +00:00
										 |  |  |         type = str; | 
					
						
							| 
									
										
										
										
											2019-08-30 15:46:57 +02:00
										 |  |  |         default = "none"; | 
					
						
							|  |  |  |         description = ''
 | 
					
						
							| 
									
										
										
										
											2019-09-03 11:02:52 +02:00
										 |  |  |           The type of the filesystem to be mounted. | 
					
						
							| 
									
										
										
										
											2020-08-07 14:43:58 +01:00
										 |  |  |           Linux: filesystem types supported by the kernel as listed in | 
					
						
							|  |  |  |           `/proc/filesystems` (e.g., "minix", "ext2", "ext3", "jfs", "xfs", | 
					
						
							|  |  |  |           "reiserfs", "msdos", "proc", "nfs", "iso9660"). For bind mounts | 
					
						
							| 
									
										
										
										
											2019-09-03 11:02:52 +02:00
										 |  |  |           (when options include either bind or rbind), the type is a dummy, | 
					
						
							|  |  |  |           often "none" (not listed in /proc/filesystems). | 
					
						
							|  |  |  |         '';
 | 
					
						
							| 
									
										
										
										
											2019-08-30 15:46:57 +02:00
										 |  |  |       }; | 
					
						
							|  |  |  |       source = mkOption { | 
					
						
							| 
									
										
										
										
											2019-09-06 14:50:59 +00:00
										 |  |  |         type = str; | 
					
						
							| 
									
										
										
										
											2019-08-30 15:46:57 +02:00
										 |  |  |         description = "Source for the in-container mount"; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       options = mkOption { | 
					
						
							| 
									
										
										
										
											2020-08-23 01:28:45 +02:00
										 |  |  |         type = attrsOf (str); | 
					
						
							| 
									
										
										
										
											2019-08-30 15:46:57 +02:00
										 |  |  |         default = [ "bind" ]; | 
					
						
							|  |  |  |         description = ''
 | 
					
						
							| 
									
										
										
										
											2019-09-03 11:02:52 +02:00
										 |  |  |           Mount options of the filesystem to be used. | 
					
						
							| 
									
										
										
										
											2020-08-07 14:43:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-07 17:58:02 +02:00
										 |  |  |           Support options are listed in the mount(8) man page. Note that | 
					
						
							| 
									
										
										
										
											2020-08-07 14:43:58 +01:00
										 |  |  |           both filesystem-independent and filesystem-specific options | 
					
						
							| 
									
										
										
										
											2019-09-03 11:02:52 +02:00
										 |  |  |           are listed. | 
					
						
							|  |  |  |         '';
 | 
					
						
							| 
									
										
										
										
											2019-08-30 15:46:57 +02:00
										 |  |  |       }; | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-09-03 11:02:52 +02:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-08-30 15:46:57 +02:00
										 |  |  | in | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   options.services.railcar = { | 
					
						
							|  |  |  |     enable = mkEnableOption "railcar"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     containers = mkOption { | 
					
						
							|  |  |  |       default = {}; | 
					
						
							|  |  |  |       description = "Declarative container configuration"; | 
					
						
							| 
									
										
										
										
											2020-08-23 01:28:45 +02:00
										 |  |  |       type = with types; attrsOf (submodule ({ name, config, ... }: { | 
					
						
							| 
									
										
										
										
											2019-08-30 15:46:57 +02:00
										 |  |  |         options = { | 
					
						
							|  |  |  |           cmd = mkOption { | 
					
						
							| 
									
										
										
										
											2019-09-06 14:50:59 +00:00
										 |  |  |             type = types.lines; | 
					
						
							| 
									
										
										
										
											2019-08-30 15:46:57 +02:00
										 |  |  |             description = "Command or script to run inside the container"; | 
					
						
							|  |  |  |           }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           mounts = mkOption { | 
					
						
							| 
									
										
										
										
											2019-09-03 11:02:52 +02:00
										 |  |  |             type = with types; attrsOf mount; | 
					
						
							| 
									
										
										
										
											2019-08-30 15:46:57 +02:00
										 |  |  |             default = {}; | 
					
						
							|  |  |  |             description = ''
 | 
					
						
							|  |  |  |               A set of mounts inside the container. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               The defaults have been chosen for simple bindmounts, meaning | 
					
						
							|  |  |  |               that you only need to provide the "source" parameter. | 
					
						
							|  |  |  |             '';
 | 
					
						
							|  |  |  |             example = ''
 | 
					
						
							|  |  |  |               { "/data" = { source = "/var/lib/data"; }; } | 
					
						
							|  |  |  |             '';
 | 
					
						
							|  |  |  |           }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           runType = mkOption { | 
					
						
							| 
									
										
										
										
											2019-09-06 14:50:59 +00:00
										 |  |  |             type = types.str; | 
					
						
							| 
									
										
										
										
											2019-08-30 15:46:57 +02:00
										 |  |  |             default = "oneshot"; | 
					
						
							|  |  |  |             description = "The systemd service run type"; | 
					
						
							|  |  |  |           }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           os = mkOption { | 
					
						
							| 
									
										
										
										
											2019-09-06 14:50:59 +00:00
										 |  |  |             type = types.str; | 
					
						
							| 
									
										
										
										
											2019-08-30 15:46:57 +02:00
										 |  |  |             default = "linux"; | 
					
						
							|  |  |  |             description = "OS type of the container"; | 
					
						
							|  |  |  |           }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           arch = mkOption { | 
					
						
							| 
									
										
										
										
											2019-09-06 14:50:59 +00:00
										 |  |  |             type = types.str; | 
					
						
							| 
									
										
										
										
											2019-08-30 15:46:57 +02:00
										 |  |  |             default = "x86_64"; | 
					
						
							|  |  |  |             description = "Computer architecture type of the container"; | 
					
						
							|  |  |  |           }; | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |       })); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     stateDir = mkOption { | 
					
						
							|  |  |  |       type = types.path; | 
					
						
							| 
									
										
										
										
											2021-01-24 09:19:10 +00:00
										 |  |  |       default = "/var/railcar"; | 
					
						
							| 
									
										
										
										
											2019-08-30 15:46:57 +02:00
										 |  |  |       description = "Railcar persistent state directory"; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     package = mkOption { | 
					
						
							|  |  |  |       type = types.package; | 
					
						
							|  |  |  |       default = pkgs.railcar; | 
					
						
							|  |  |  |       description = "Railcar package to use"; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   config = mkIf cfg.enable { | 
					
						
							|  |  |  |     systemd.services = flip mapAttrs' cfg.containers (name: containerConfig: | 
					
						
							|  |  |  |       generateUnit name containerConfig | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |