Files
DesktopAnsible/roles/nix/tasks/main.yml
2023-06-30 08:35:59 -04:00

109 lines
3.4 KiB
YAML

---
- name: Get nix version
shell: |
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
nix --version
ignore_errors: true
changed_when: false
register: nix_version_output
- name: Set nix version name
set_fact:
nix_version_on_system: "{{ (nix_version_output.stdout_lines[0] | split(' '))[2] }}"
when: nix_version_output.rc == 0
- name: Download and run installer
block:
- name: Download installer
get_url:
url: "{{ installer_path }}"
dest: /tmp
checksum: "{{ installer_checksum }}"
- name: extract installer
unarchive:
src: /tmp/{{ nix_build }}.tar.xz
remote_src: true
dest: /tmp/
- name: Uninistall the previous nix
block:
- name: disable nix socket
service:
name: nix-daemon.socket
state: stopped
enabled: false
become: true
ignore_errors: true
- name: disable nix service
service:
name: nix-daemon.service
state: stopped
enabled: false
become: true
ignore_errors: true
- name: Remove Installed nix files
file:
state: absent
path: /nix
become: true
- name: Remove Installed nix config files
file:
state: absent
path: /etc/nix
become: true
- name: stat bash.bashrc.backup-before-nix
stat:
path: /etc/bash.bashrc.backup-before-nix
register: bash_bashrc_backup_stat
- name: Restore bash.bashrc
shell: mv /etc/bash.bashrc.backup-before-nix /etc/bash.bashrc
become: true
when: bash_bashrc_backup_stat.stat.exists
- name: stat bashrc.backup-before-nix
stat:
path: /etc/bashrc.backup-before-nix
register: bashrc_backup_stat
- name: Restore bashrc
shell: mv /etc/bashrc.backup-before-nix /etc/bashrc
become: true
when: bashrc_backup_stat.stat.exists
- name: stat zshrc.backup-before-nix
stat:
path: /etc/zshrc.backup-before-nix
register: zshrc_backup_stat
- name: Restore zshrc
shell: mv /etc/zshrc.backup-before-nix /etc/zshrc
become: true
when: zshrc_backup_stat.stat.exists
- name: stat nix.sh
stat:
path: /etc/profile.d/nix.sh.backup-before-nix
register: nixsh_backup_stat
- name: Restore nix.sh
shell: mv /etc/profile.d/nix.sh.backup-before-nix /etc/profile.d/nix.sh
become: true
when: nixsh_backup_stat.stat.exists
when: nix_version_on_system is defined and nix_version_on_system != nix_version
- name: Run the installer
become: true
ansible.builtin.shell:
cmd: ./install --daemon </dev/null
chdir: /tmp/{{ nix_build }}
when: (not nix_version_on_system is defined) or nix_version_on_system != nix_version
- name: Enable flakes
become: true
lineinfile:
path: /etc/nix/nix.conf
line: "experimental-features = nix-command flakes"
when: flakes
- name: Add arbritrary nix config
blockinfile:
path: /etc/nix/nix.conf
block: "{{ config }}"
become: true
when: config is defined
- name: run nix commands
shell: |
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
{{ item }}
changed_when: true
loop: "{{ nix_commands }}"