Home Explore Blog CI



nixpkgs

2nd chunk of `doc/build-helpers/images/portableservice.section.md`
60b5b0f9088f6d1bb1e2c3d100ab7f65a5a0d2a7cc033a8400000001000008e2
  For each item in the list, `portableService` will create a symlink in the path specified by `symlink` (relative to the root of the image) that points to `object`.

  All packages that `object` depends on and their dependencies are automatically copied into the image.

  This can be used to create symlinks for applications that assume some files to exist globally (`/etc/ssl` or `/bin/bash`, for example).
  See [](#ex-portableService-symlinks) to understand how to do that.

  _Default value:_ `[]`.

`contents` (List of Attribute Set; _optional_)

: A list of additional derivations to be included as-is in the image.
  These derivations will be included directly in a `/nix/store` directory inside the image.

  _Default value:_ `[]`.

`squashfsTools` (Attribute Set; _optional_)

: Allows you to override the package that provides {manpage}`mksquashfs(1)`, which is used internally by `portableService`.

  _Default value:_ `pkgs.squashfsTools`.

`squash-compression` (String; _optional_)

: Passed as the compression option to {manpage}`mksquashfs(1)`, which is used internally by `portableService`.

  _Default value:_ `"xz -Xdict-size 100%"`.

`squash-block-size` (String; _optional_)

: Passed as the block size option to {manpage}`mksquashfs(1)`, which is used internally by `portableService`.

  _Default value:_ `"1M"`.

## Examples {#ssec-pkgs-portableService-examples}

[]{#ex-pkgs-portableService}
:::{.example #ex-portableService-hello}
# Building a Portable Service image

The following example builds a Portable Service image with the `hello` package, along with a service unit that runs it.

```nix
{
  lib,
  writeText,
  portableService,
  hello,
}:
let
  hello-service = writeText "hello.service" ''
    [Unit]
    Description=Hello world service

    [Service]
    Type=oneshot
    ExecStart=${lib.getExe hello}
  '';
in
portableService {
  pname = "hello";
  inherit (hello) version;
  units = [ hello-service ];
}
```

After building the package, the generated image can be loaded into a system through {manpage}`portablectl(1)`:

```shell
$ nix-build
(some output removed for clarity)
/nix/store/8c20z1vh7z8w8dwagl8w87b45dn5k6iq-hello-img-2.12.1

$ portablectl attach /nix/store/8c20z1vh7z8w8dwagl8w87b45dn5k6iq-hello-img-2.12.1/hello_2.12.1.raw

Title: pkgs.portableService: Optional Attributes and Examples
Summary
This section details the optional attributes for `pkgs.portableService`, including `contents` for adding derivations to the image, `squashfsTools` for overriding the squashfs tool package, and `squash-compression` and `squash-block-size` for configuring squashfs compression. It also provides an example of building a Portable Service image using the `hello` package and a systemd service unit, demonstrating how to use `portablectl` to attach the generated image to a system.