# Installing RefineID

RefineID provides the `refineid` CLI, the desktop GUI (`refineid-gui`),
and the PKCS#11 module (`librefineid_pkcs11.so`) for Finnish identity cards.

---

## 1. Debian & Ubuntu (.deb package)

The recommended way to install RefineID on Ubuntu / Debian is to build
and install the standalone `.deb` package using the single packaging script.

### 1a. Install Build Dependencies

**1. System C libraries and headers:**
```sh
sudo apt update
sudo apt install -y \
  build-essential \
  cmake \
  pkg-config \
  libpcsclite-dev \
  libgtk-4-dev \
  libfontconfig1-dev \
  libxkbcommon-dev \
  libx11-dev \
  libxcursor-dev \
  libxi-dev \
  libxrandr-dev \
  pcscd \
  libccid \
  p11-kit
```

Pairing with a phone keeps each pairing's record, its private key included,
in one owner-only file (mode `0600`, in a directory created with mode `0700`)
under `$XDG_CONFIG_HOME/refineid/rapp-pairs`, by default
`~/.config/refineid/rapp-pairs`. No keyring is used.

**2. Rust Toolchain (Rust 1.95+ required):**
RefineID uses the Rust 2024 edition and requires Rust 1.95 or newer. If your distribution's packaged `rustc` is older than 1.95 (e.g., Ubuntu 24.04 / 26.04 default packages), install or update Rust via [rustup](https://rustup.rs):
```sh
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source "$HOME/.cargo/env"
```

### 1b. Build the Package
Run the single build script:
```sh
script/package-deb.sh
```

The script compiles the release binaries and builds a `.deb` archive in
`target/deb/refineid_<version>_<arch>.deb`.

### 1c. Install the Package
```sh
sudo apt install ./target/deb/refineid_*.deb
```

*What the package configures automatically:*
- Installs `refineid` and `refineid-gui` to `/usr/bin/`, and the
  manual page refineid(1)
- Installs the PKCS#11 module to `/usr/lib/librefineid_pkcs11.so`
- Registers the desktop application entry and icon in `/usr/share/`
- Configures `p11-kit` (`/usr/share/p11-kit/modules/refineid.module`) so
  system crypto consumers (GnuTLS, OpenSSL pkcs11-provider, OpenSSH)
  discover the card
- Configures Firefox enterprise policies (`/etc/firefox/policies/policies.json`)
  for automatic card login
- Enables and activates the smart-card daemon socket (`pcscd.socket`)

*Firefox from snap:* Ubuntu's default Firefox is a snap, and its
confinement denies loading a module from the host's `/usr/lib`, a
symlink into it included. When `/var/snap/firefox/common` exists, the
package keeps a copy of the module at
`/var/snap/firefox/common/refineid/librefineid_pkcs11.so`, refreshed on
every install and upgrade and removed with the package, and the Firefox
policy names both paths; Firefox ignores the one it cannot load. Install
the package after the snap, or reinstall it, for the copy to appear.

### 1d. Uninstalling
To cleanly remove the package and all its integrations:
```sh
sudo apt remove refineid
```

To also remove configuration files:
```sh
sudo apt purge refineid
```

---

## 2. NixOS

### 2a. Automated Deployment
Deploy and verify RefineID on a target NixOS system (locally or over SSH, defaults to `root@nixos`):
```sh
script/deploy-nixos.sh [TARGET]
# or: make deploy-nixos [TARGET=root@nixos]
```
The script automatically configures `/etc/nixos/configuration.nix` if RefineID is not yet enabled, rebuilds the system, verifies the CLI, GUI, and `pcscd` daemon, and checks the Firefox enterprise policy.

### 2b. Declarative Configuration
Add the flake module and enable RefineID in `/etc/nixos/configuration.nix`:
```nix
  imports = [
    ./hardware-configuration.nix
    ((builtins.getFlake "github:refineid/refineid-unix").nixosModules.default)
  ];
  programs.refineid.enable = true;
```
Then rebuild:
```sh
sudo nixos-rebuild switch
```
See [doc/install-nixos.md](doc/install-nixos.md) for complete instructions, flake configurations, card login, and troubleshooting.

---

## 3. Generic Linux (Source Build)

### 3a. Build from Source
Ensure Rust 1.95+ and development libraries for `pcsc-lite`, `gtk3`, and
`fontconfig` are installed, then run:

```sh
cargo build --release --workspace
```

Binaries will be placed in `target/release/`:
- `target/release/refineid`
- `target/release/refineid-gui`
- `target/release/librefineid_pkcs11.so`

### 3b. Manual System Installation
```sh
# Binaries
sudo install -m 755 target/release/refineid /usr/local/bin/refineid
sudo install -m 755 target/release/refineid-gui /usr/local/bin/refineid-gui

# Manual page and zsh completion
sudo install -d /usr/local/share/man/man1 /usr/local/share/zsh/site-functions
sudo install -m 644 crates/refineid-client/man/refineid.1 /usr/local/share/man/man1/refineid.1
sudo install -m 644 packaging/zsh/_refineid /usr/local/share/zsh/site-functions/_refineid

# PKCS#11 shared module
sudo install -m 755 target/release/librefineid_pkcs11.so /usr/local/lib/librefineid_pkcs11.so
sudo ldconfig

# Desktop entry & icon
sudo install -d /usr/local/share/applications /usr/local/share/icons/hicolor/scalable/apps
sudo install -m 644 crates/refineid-gui/assets/app-icon.svg /usr/local/share/icons/hicolor/scalable/apps/refineid.svg
sudo install -m 644 -T <(cat << 'DESKTOPEOF'
[Desktop Entry]
Type=Application
Name=RefineID
GenericName=Identity card tool
Comment=Finnish identity card: document signing, PIN management, phone pairing
Exec=/usr/local/bin/refineid-gui
Icon=refineid
Terminal=false
Categories=Utility;Security;
Keywords=FINEID;smartcard;PIN;identity;signing;
DESKTOPEOF
) /usr/local/share/applications/refineid.desktop

# p11-kit registration
sudo install -d /etc/pkcs11/modules
sudo tee /etc/pkcs11/modules/refineid.module > /dev/null << 'P11EOF'
module: /usr/local/lib/librefineid_pkcs11.so
trust-policy: no
critical: no
P11EOF

# Enable smart-card daemon
sudo systemctl enable --now pcscd.socket
```

---

## 4. NetBSD

NetBSD 11.0/aarch64 builds the whole workspace including the Slint GUI.
See [doc/install-netbsd.md](doc/install-netbsd.md) for the bring-up order
(base X sets, pkgsrc packages, pcscd, VNC desktop, Openbox menu).

## 5. OpenBSD

OpenBSD 8.0/amd64 builds the CLI, the PKCS#11 module and the FLTK GUI from
packages. See [doc/install-openbsd.md](doc/install-openbsd.md), including
the CDE Application Manager entry.

## 6. Verifying the Installation

1. **Reader and Card Status**:
   ```sh
   pcsc_scan
   ```
2. **Read Card Metadata**:
   ```sh
   refineid card
   ```
3. **Check PKCS#11 Module Discovery**:
   ```sh
   p11-kit list-modules
   ```
