|
| 1 | +--- |
| 2 | +- name: Import optional extra_args file |
| 3 | + ansible.builtin.include_vars: "{{ item }}" |
| 4 | + ignore_errors: true |
| 5 | + with_first_found: |
| 6 | + - files: |
| 7 | + - "../extra_vars.yml" |
| 8 | + - "../extra_vars.yaml" |
| 9 | + - "../extra_vars.json" |
| 10 | + skip: true |
| 11 | + tags: vars |
| 12 | + |
| 13 | +- name: Fail if nix cache mirror is enabled but user is not root |
| 14 | + ansible.builtin.fail: |
| 15 | + msg: "Nix cache mirror setup requires root privileges. Please run as root." |
| 16 | + when: |
| 17 | + - install_nix_cache_mirror | bool |
| 18 | + - ansible_user_id != 'root' |
| 19 | + tags: ["nix-cache", "mirror"] |
| 20 | + |
| 21 | +- name: Install nginx for Nix cache mirror |
| 22 | + become: true |
| 23 | + ansible.builtin.package: |
| 24 | + name: |
| 25 | + - nginx |
| 26 | + - curl |
| 27 | + state: present |
| 28 | + when: install_nix_cache_mirror | bool |
| 29 | + tags: ["nix-cache", "mirror"] |
| 30 | + |
| 31 | +- name: Create Nix cache mirror directory |
| 32 | + become: true |
| 33 | + ansible.builtin.file: |
| 34 | + path: "{{ nix_cache_mirror_path }}" |
| 35 | + state: directory |
| 36 | + mode: "0755" |
| 37 | + owner: www-data |
| 38 | + group: www-data |
| 39 | + when: install_nix_cache_mirror | bool |
| 40 | + tags: ["nix-cache", "mirror"] |
| 41 | + |
| 42 | +- name: Template nginx configuration for Nix cache mirror |
| 43 | + become: true |
| 44 | + ansible.builtin.template: |
| 45 | + src: nix-cache-mirror.nginx.j2 |
| 46 | + dest: "{{ nix_cache_mirror_nginx_conf_path }}" |
| 47 | + mode: "0644" |
| 48 | + owner: root |
| 49 | + group: root |
| 50 | + when: install_nix_cache_mirror | bool |
| 51 | + notify: reload nginx |
| 52 | + tags: ["nix-cache", "mirror"] |
| 53 | + |
| 54 | +- name: Enable nginx site for Nix cache mirror |
| 55 | + become: true |
| 56 | + ansible.builtin.file: |
| 57 | + src: "{{ nix_cache_mirror_nginx_conf_path }}" |
| 58 | + dest: "{{ nix_cache_mirror_nginx_enabled_path }}" |
| 59 | + state: link |
| 60 | + when: install_nix_cache_mirror | bool |
| 61 | + notify: reload nginx |
| 62 | + tags: ["nix-cache", "mirror"] |
| 63 | + |
| 64 | +- name: Remove default nginx site |
| 65 | + become: true |
| 66 | + ansible.builtin.file: |
| 67 | + path: /etc/nginx/sites-enabled/default |
| 68 | + state: absent |
| 69 | + when: install_nix_cache_mirror | bool |
| 70 | + notify: reload nginx |
| 71 | + tags: ["nix-cache", "mirror"] |
| 72 | + |
| 73 | +- name: Create systemd service for Nix cache syncer |
| 74 | + become: true |
| 75 | + ansible.builtin.template: |
| 76 | + src: nix-cache-sync.service.j2 |
| 77 | + dest: /etc/systemd/system/nix-cache-sync.service |
| 78 | + mode: "0644" |
| 79 | + when: install_nix_cache_mirror | bool |
| 80 | + notify: reload systemd |
| 81 | + tags: ["nix-cache", "mirror"] |
| 82 | + |
| 83 | +- name: Create systemd timer for Nix cache syncer |
| 84 | + become: true |
| 85 | + ansible.builtin.template: |
| 86 | + src: nix-cache-sync.timer.j2 |
| 87 | + dest: /etc/systemd/system/nix-cache-sync.timer |
| 88 | + mode: "0644" |
| 89 | + when: install_nix_cache_mirror | bool |
| 90 | + notify: reload systemd |
| 91 | + tags: ["nix-cache", "mirror"] |
| 92 | + |
| 93 | +- name: Enable and start nginx service |
| 94 | + become: true |
| 95 | + ansible.builtin.systemd: |
| 96 | + name: nginx |
| 97 | + enabled: true |
| 98 | + state: started |
| 99 | + daemon_reload: true |
| 100 | + when: install_nix_cache_mirror | bool |
| 101 | + tags: ["nix-cache", "mirror"] |
| 102 | + |
| 103 | +- name: Enable and start nix-cache-sync timer |
| 104 | + become: true |
| 105 | + ansible.builtin.systemd: |
| 106 | + name: nix-cache-sync.timer |
| 107 | + enabled: true |
| 108 | + state: started |
| 109 | + daemon_reload: true |
| 110 | + when: install_nix_cache_mirror | bool |
| 111 | + tags: ["nix-cache", "mirror"] |
| 112 | + |
| 113 | +- name: Check if firewalld is running |
| 114 | + ansible.builtin.command: systemctl is-active firewalld |
| 115 | + register: firewalld_status |
| 116 | + ignore_errors: true |
| 117 | + when: |
| 118 | + - install_nix_cache_mirror | bool |
| 119 | + - linux_mirror_nfs | bool |
| 120 | + tags: ["nix-cache", "mirror"] |
| 121 | + |
| 122 | +- name: Open firewall for Nix cache mirror HTTP traffic |
| 123 | + become: true |
| 124 | + ansible.posix.firewalld: |
| 125 | + port: "{{ nix_cache_mirror_port }}/tcp" |
| 126 | + permanent: true |
| 127 | + state: enabled |
| 128 | + immediate: true |
| 129 | + when: |
| 130 | + - install_nix_cache_mirror | bool |
| 131 | + - linux_mirror_nfs | bool |
| 132 | + - firewalld_status.rc == 0 |
| 133 | + tags: ["nix-cache", "mirror"] |
0 commit comments