Skip to content

Latest commit

 

History

History
84 lines (66 loc) · 2.4 KB

20-using-modules.org

File metadata and controls

84 lines (66 loc) · 2.4 KB

Using modules

Let’s cover some examples of NixOS modules

#1 - systemd unit


{ pkgs, ... }:
{
  systemd.services.helloService = {
    enable = true;
    serviceConfig = {
      ExecStart = ''
        ${pkgs.hello}/bin/hello -g "Hello, $company_name!"
      '';
      Type = "oneshot";
    };
  };
}

Use a deloy mechanism of your choice to active the configuration.

  • sudo nixos-rebuild switch
  • nix build -f <nixpkgs/nixos> system && result/bin/switch-to-configuration
  • or as a VM: nix build -f '<nixpkgs/nixos>' vm -I nixos-config=config.nix

 ❤ (theia) ~> sudo systemctl status helloService.service
● helloService.service
Loaded: loaded (/nix/store/.../helloService.service)
Active: inactive (dead)
 ❤ (theia) ~> sudo journalctl -u helloService.service
-- Logs begin at Sat 2020-10-24 16:54:30 CEST, end at Tue 2021-01-19 15:37:37 CET. --
Jan 19 15:36:38 uwu systemd[1]: Starting helloService.service...
Jan 19 15:36:38 uwu hello[1302]: Hello, $company_name!
Jan 19 15:36:38 uwu systemd[1]: helloService.service: Succeeded.
Jan 19 15:36:38 uwu systemd[1]: Finished helloService.service.

#2 - user management


Create a user with a SSH key access.

The NixOS users module also ensures that users are created, and retired (UIDs remains in-use!) when configuration is removed.

{ config, ... }:
{
  services.openssh.enable = true;
  users.users.alice = {
    createHome = true;
    isNormalUser = true;
    openssh.authorizedKeys.keys = [ "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOSOwKTQavB5TovmD85RMBw8to5+tfSXfzSAwZXcp+Yg" ];
  };
}

  • Build with nix build -f '<nixpkgs/nixos>' system
  • Check result link for outputs
  • When activating the configuration etc link is replaced on the system with the new version
(theia) ~> ls result/etc/ssh/authorized_keys.d/
alice alice.mode alice.gid alice.uid(theia) ~> cat result/etc/ssh/authorized_keys.d/alice
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOSOwKTQavB5TovmD85RMBw8to5+tfSXfzSAwZXcp+Yg