# NixOS

# VPN to FRITZ!Box from Linux

# Requirements

- Packages: vpnc, network-manager-vpnc und network-manager-vpnc-gnome
- MyFRITZ!-address of the FRITZ!Box:  
    **Example**: pi80ewgfi72d2os42.myfritz.net
- Username of the FRITZ!Box-User:  
    **Example**: Max Mustermann
- Password of the FRITZ!Box-User:  
    **Example**: geheim1234
- Shared Secret of the FRITZ!Box-User:  
    **Example**: Zj7hPCouK65IrPU4

# Preparations

## Setup MyFRITZ!

Register FRITZ!Box at MyFRITZ! for a fixed MyFRITZ!-address. [Guide in German](https://ch.avm.de/service/wissensdatenbank/dok/FRITZ-Box-7390-A-CH/966_MyFRITZ-Konto-erstellen-und-in-FRITZ-Box-einrichten/)

## Adjust the IP-Network of the FRITZ!Box

Setup the IP-Address for conecting with the FRITZ!Box:

1. Click on "Heimnetz".
2. Click on "Heimnetzübersicht".
3. Click on "Netzwerkeinstellungen".
4. Click on "IPv4-Adressen". If not visible, you have to activate the extended view.
5. Enter the IP-Address.
6. Save the settings.

# Setup the VPN-Connecttion in the FRITZ!Box

Create an own user for every VPN-Connection:

1. Click on "System in the FRITZ!Box.
2. Click on "FRITZ!Box-Benutzer".
3. Edit the prefered user (or create a new one), and setup the vpn connection: 
    1. Click on "Benutzer hinzufügen".
    2. Enter the username and the password.
4. Activate the option "VPN".
5. Save the settings.

# Setuo the VPN-Connection on your device

1. Start Advanced Network Configuration.
2. Click on the plus symbol and select ""Cisco-kompatibler VPN-Client (vpnc)" aus.
3. Enter the wanted Name.
4. Enter the MyFritz!-Address field(pi80ewgfi72d2os42.myfritz.net) in the "Gateway".
5. Enter the FritzBox!-Username and -Password and select the option "Passwort nur für diesen Benutzer speichern". aus und tragen Sie dann das Kennwort des FRITZ!Box-Benutzers (geheim1234) ein.
6. nter the FritzBox!-Username also in the field "Gruppenname" and select the option "Passwort nur für diesen Benutzer speichern".
7. Enter the "Shared Secret" aus und tragen Sie dann das "Shared Secret".
8. Click on "Extended".
9. Enter "tun0" as "Tunnel-Schnittstellenname".
10. Save the settings.

# Quelle

[Tutorial VPN zur FritzBox einrichten](https://ch.avm.de/service/wissensdatenbank/dok/FRITZ-Box-7390-A-CH/1471_VPN-zur-FRITZ-Box-unter-Linux-einrichten/)

# Install

We follow the **UEFI** part of the official Installation Guide:  
[NixOS Installation Guide](https://nixos.wiki/wiki/NixOS_Installation_Guide).

# Prepare Disk

## Partitioning

Partition the storage using `sudo fdisk /dev/sdb`. In the interactive prompt partition the storage those settings:

- `g` (gpt disk label)
- `n`
- `1` (partition number \[1/128\])
- `2048` first sector
- `+500M` last sector (boot sector size)
- `t`
- `1` (EFI System)
- `n`
- `2`
- default (fill up partition)
- default (fill up partition)
- `w` (write)

Delete all Files on partitions with:

```
dd if=/dev/zero of=/dev/sdb1 bs=4096 status=progress
dd if=/dev/zero of=/dev/sdb2 bs=4096 status=progress

```

## Label partitions

Label the partitions using those commands:

```
sudo mkfs.fat -F 32 /dev/sda1
sudo fatlabel /dev/sda1 NIXBOOT
sudo mkfs.ext4 /dev/sda2 -L NIXROOT

```

## Mount partitions

Mount the boot and root drives so we can access them and install NixOS:

```
sudo mount /dev/disk/by-label/NIXROOT /mnt
sudo mkdir -p /mnt/boot
sudo mount /dev/disk/by-label/NIXBOOT /mnt/boot

```

## Create swap file

```
sudo dd if=/dev/zero of=/mnt/.swapfile bs=1024 count=2097152 # 2GB size
sudo chmod 600 /mnt/.swapfile
sudo mkswap /mnt/.swapfile
sudo swapon /mnt/.swapfile

```

# Install Nixos

## Create NixOS config

Generate the config using `sudo nixos-generate-config --root /mnt`.

Then, edit the config using `sudo vim /mnt/etc/nixos/configuration.nix`.

Here are some sections of the configuration you should add:

```
  # Keyboard layout
  services.xserver.xkb.layout = "de";

  # Add a user!
  users.users.elias = {
    isNormalUser = true;
    extraGroups = [ "wheel" ]; # Sudo access
  };

  # Install an editor to edit the configuration
  environment.systemPackages = with pkgs; [ vim ]; # or vim!

```

To edit the hardware config, use `sudo vim /mnt/etc/nixos/hardware-configuration.nix`.

You can then update the file systems to use labels and add the swapfile.

```
  fileSystems."/" =
    { device = "/dev/disk/by-label/NIXROOT";
      # ...
    };

  fileSystems."/boot" =
    { device = "/dev/disk/by-label/NIXBOOT";
      # ...
    };

  swapDevices = [{
    device = "/.swapfile";
    size = 2048; # 2GB
  }];

```

## Install

```
cd /mnt
sudo nixos-install

```

Change password with `passwd` to a password that works on `de` and `en` keyboard layout.

Reboot into the installed NixOs.

Add those lines to the `configuration.nix` file:

```
nix.settings.experimental-features = ["nix-command" "flakes" ];

networking.hostName = "eliasDesktop"; #or eiasLaptop for other configs

environment.systemPackages = with pkgs; [
  vim
  git
];

```

Rebuild the system with `sudo nixos-rebuild switch` and reboot to update the Hostname.

Copy the config repo with `git clone https://github.com/4Lost/nixos-config` and move it to `/etc/nixos` and rebuild with `sudo nixos-rebuild switch`.

Make the directory usable for push and pull:

```
sudo chown -R elias /etc/nixos

```