This commit is contained in:
2023-06-30 08:29:42 -04:00
parent 4b51ce2bae
commit c8431fb979
4 changed files with 116 additions and 1 deletions

View File

@@ -4,7 +4,7 @@
become: true
roles:
- danielrolls.nix
- nix
- packages
# - customize
# - ansibleuser

View File

@@ -0,0 +1,5 @@
---
nix_version: 2.16.1
installer_checksum: sha512:4edf245baf9641843b6bb750a1889e0266bace6df03cf90845c9c6279db4a3c2d1d100de446550658be84ac9b6b15017b05cd792cb6ee5da7ab74b79b215fd47
flakes: false
nix_commands: []

107
roles/nix/tasks/main.yml Normal file
View File

@@ -0,0 +1,107 @@
- 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] | cut -d' ' -f3"
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 }}"

3
roles/nix/vars/main.yml Normal file
View File

@@ -0,0 +1,3 @@
---
nix_build: nix-{{ nix_version }}-x86_64-linux
installer_path: https://releases.nixos.org/nix/nix-{{ nix_version }}/{{ nix_build }}.tar.xz