Prateek Raj Gautam
Prateek Raj Gautam

Reputation: 27

How to Enable cloudflared on nixos? using configuration.nix

I am using Nixos, how can I use Cloudflare tunnel in a declarative way?

Upvotes: 0

Views: 500

Answers (1)

Prateek Raj Gautam
Prateek Raj Gautam

Reputation: 27

Create a nix file as /etc/nixos/cloudflared.nix

sudo vim /etc/nixos/cloudflared.nix

paste following code

press :wq to save and exit

file /etc/nixos/cloudflared.nix

{ config, lib, pkgs, ... }:

{
environment.systemPackages = [ pkgs.cloudflared ];




 users.users.cloudflared = {
    group = "cloudflared";
    isSystemUser = true;
  };
  users.groups.cloudflared = { };
  
  
    systemd.services.my_tunnel = {
    wantedBy = [ "multi-user.target" ];
#    after = [ "network.target" ];
    after = [ "network-online.target" "systemd-resolved.service" ];
    serviceConfig = {
      ExecStart = "${pkgs.cloudflared}/bin/cloudflared tunnel --no-autoupdate run --token=<myToken>";
#      ExecStart = "${pkgs.cloudflared}/bin/cloudflared tunnel --no-autoupdate run --credentials-file=/home/prateek/cloudflare.token";
      Restart = "always";
      User = "cloudflared";
      Group = "cloudflared";
    };
  };

}

verify file content with

cat /etc/nixos/cloudflared.nix

and

import in /etc/nixos/configuration.nix


{ pkgs, lib, config, ... }:
#let
#    sources = import ./npins;
#    proxmox-nixos = import sources.proxmox-nixos;
#in
{
  imports = [ 
                ./hardware-configuration.nix
                ./cloudflare.nix
              
            ];


#...
# rest of config
#...

Upvotes: 0

Related Questions