NixOS / nix

Nix, the purely functional package manager
https://nixos.org/
GNU Lesser General Public License v2.1
13k stars 1.54k forks source link

Database upgrades are not atomic with schema number bump #1954

Open nico202 opened 6 years ago

nico202 commented 6 years ago

Report

I'm on NixOS unstable. After nixos-rebuild switch'ing to the latest unstable, (upgrade from the unstable of last week), nix does not work anymore, neither as root nor as user. The error message:

nix-shell -p # same as sudo nixos-rebuild switch
error: cannot open connection to remote store 'daemon': executing SQLite statement 'alter table ValidPaths add column ultimate integer': SQL logic error (in '/nix/var/nix/db/db.sqlite')        
(use '--show-trace' to show detailed location information)

The same error if run with sudo or after sudo -s.

Details

nix-info as user (those don's seems to be channels, see the output of nix-channel --list below):

nix-info as root: error: executing SQLite statement 'alter table ValidPaths add column ultimate integer': SQL logic error (in '/nix/var/nix/db/db.sqlite')

Both as user and root: nix-channel --list nixos https://nixos.org/channels/nixos-unstable

cat /etc/nix/nix.conf

# WARNING: this file is generated from the nix.* options in                                                                                                                                      
# your NixOS configuration, typically
# /etc/nixos/configuration.nix.  Do not edit it!
build-users-group = nixbld
max-jobs = 4
cores = 1
sandbox = true
extra-sandbox-paths =  
substituters = https://cache.nixos.org/
trusted-substituters = 
trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=
auto-optimise-store = false
require-sigs = true

trusted-users = root
allowed-users = *
builders =

nix-store --verify --check-contents --readonly-mode

root:

error: executing SQLite statement 'alter table ValidPaths add column ultimate integer': SQL logic error (in '/nix/var/nix/db/db.sqlite')

user:

error: cannot open connection to remote store 'daemon': executing SQLite statement 'alter table ValidPaths add column ultimate integer': SQL logic error (in '/nix/var/nix/db/db.sqlite')

infinisil commented 6 years ago

Ping @edolstra, this seems very bad

shlevy commented 6 years ago

@NixOS/nix-core FYI

nico202 commented 6 years ago

I'm waiting to re-install nixos from a live usb to help this get fixed, so please let me know of any missing information you need, thanks :)

shlevy commented 6 years ago

@nico202 What version of nix did you have before?

nico202 commented 6 years ago

@shlevy ehm, no idea how to find out. I've always been on latest unstable, and run sudo nix-collect-garbage -d before upgrading. This is what I have in /nix/store now:

ls /nix/store|grep nix-1.|grep -v .drv|grep -v man

0l1dl400k2a5b2r1r6jci113gwyrgqyh-nix-1.11.16                                                                                                                                                     
27llrm6fwf5yl30907zczdg6cn3jvw55-nix-1.11.15
85g53pzfnq6vsn233i2jzjf0ixmh0qd8-nix-1.11.8
866pyym5zgvx78kr55pwas16xp1m0sjp-nix-1.11.11
ggj0xvnkx0r22x21z4jnz16vq6k76wx0-nix-1.11.16
jpiy120h7ik4brra9r8k26jngj3vshll-nix-1.11.16
lpy12ab10d7gxmcvfx8dg5ccj0dcb8vl-nix-1.11.15
m0ikyjfgansgrabsrgjf4kivnmi8ws6g-nix-1.12pre5413_b4b1f452
nqkha8l57hxpgzkcpdgxyxsgr389cy3z-nix-1.11.15
nsrymmfgs6isjbf9wv105dp9sf3qyqvb-nix-1.11.16
pjk50qzcs5n3x43q8hb4556g4xm22rr3-nix-1.11.7
r91f56wfami48l9zqyk16n0xmsaw0a8n-nix-1.11.15
shlevy commented 6 years ago

@nico202 Can you check /run/booted-system?

shlevy commented 6 years ago

Ah, if you gc'd before upgrading then you should still have the generation right before the upgrade too.

shlevy commented 6 years ago

Ah I bet I know what it is. The database migrations aren't atomic. Let me get you a command to verify...

shlevy commented 6 years ago

Can you do:

echo .schema ValidPaths | sqlite3 /nix/var/nix/db/db.sqlite cat /nix/var/nix/db/schema ?

shlevy commented 6 years ago

I think we need to store the schema version in the DB to avoid this.

nico202 commented 6 years ago

I'd like to, but I don't have sqlite3 installed .-. I have another nixos machine, is there a simple way to copy all the dependencies and install it manually from there?

shlevy commented 6 years ago

@nico202 Not sure what you mean by copy all the dependencies, but you can just copy db.sqlite

shlevy commented 6 years ago

@nico202 Note that I think this will be resolvable without re-installing. Just have to verify my guess.

nico202 commented 6 years ago

@shlevy ahah yeah I was thinking the other way round (moving sqlite3 to this machine XD) I'll copy the file and try. In the meanwhile, cat /nix/var/nix/db/schema = 7

nico202 commented 6 years ago
CREATE TABLE ValidPaths (
    id               integer primary key autoincrement not null,
    path             text unique not null,
    hash             text not null,
    registrationTime integer not null,
    deriver          text,
    narSize          integer
, ultimate integer, sigs text, ca text);
CREATE TRIGGER DeleteSelfRefs before delete on ValidPaths
  begin
    delete from Refs where referrer = old.id and reference = old.id;
  end;
shlevy commented 6 years ago

OK, great. So what happened here is that you got a schema upgrade saved in the DB but the schema version file was not updated (due to process getting interrupted, reboot, whatever). The quick workaround is echo -n 10 > /nix/var/nix/db/schema, I'm working on a proper code fix to avoid this in the future.

nico202 commented 6 years ago

Wow, that was easy, thanks!

nico202 commented 6 years ago

Do you want to keep this open until you push a fix or can I close it?

shlevy commented 6 years ago

I'll keep it open if you don't mind.

nico202 commented 6 years ago

Sure no problem, thanks a lot :)

fuzzy-id commented 6 years ago

This bug just hit me after an NixOS upgrade 17.09 -> 18.03. I updated the channel nix-channel --remove and nix-channel --add, did a nixos-rebuild switch --upgrade and booted into the new system. It seems to run fine, except:

[root@host01:~]# nixos-rebuild switch 
error: executing SQLite statement 'alter table ValidPaths add column ultimate integer': SQL logic error (in '/nix/var/nix/db/db.sqlite')
building Nix...
error: cannot open connection to remote store 'daemon': executing SQLite statement 'alter table ValidPaths add column ultimate integer': SQL logic error (in '/nix/var/nix/db/db.sqlite')
(use '--show-trace' to show detailed location information)
error: cannot open connection to remote store 'daemon': executing SQLite statement 'alter table ValidPaths add column ultimate integer': SQL logic error (in '/nix/var/nix/db/db.sqlite')
(use '--show-trace' to show detailed location information)
error: cannot open connection to remote store 'daemon': executing SQLite statement 'alter table ValidPaths add column ultimate integer': SQL logic error (in '/nix/var/nix/db/db.sqlite')
warning: don't know how to get latest Nix
building the system configuration...
error: cannot open connection to remote store 'daemon': executing SQLite statement 'alter table ValidPaths add column ultimate integer': SQL logic error (in '/nix/var/nix/db/db.sqlite')
(use '--show-trace' to show detailed location information)

Here the stuff asked from above:

# echo .schema ValidPaths | sqlite3 /tmp/db.sqlite               
CREATE TABLE ValidPaths (
    id               integer primary key autoincrement not null,
    path             text unique not null,
    hash             text not null,
    registrationTime integer not null,
    deriver          text,
    narSize          integer,
    ultimate         integer, -- null implies "false"
    sigs             text, -- space-separated
    ca               text -- if not null, an assertion that the path is content-addressed; see ValidPathInfo
);
CREATE TRIGGER DeleteSelfRefs before delete on ValidPaths
  begin
    delete from Refs where referrer = old.id and reference = old.id;
  end;
# cat /nix/var/nix/db/schema 
7

NOTE: Schema looks different than the one above and also different to another NixOS 18.03 I own. But the setting /nix/var/nix/db/schema to 10 saved the day for me.

shlevy commented 6 years ago

See https://github.com/NixOS/nix/pull/1956

stale[bot] commented 3 years ago

I marked this as stale due to inactivity. → More info

fricklerhandwerk commented 2 years ago

Related: